Artificial Intelligence Nanodegree

Computer Vision Capstone

Project: Facial Keypoint Detection


Welcome to the final Computer Vision project in the Artificial Intelligence Nanodegree program!

In this project, you’ll combine your knowledge of computer vision techniques and deep learning to build and end-to-end facial keypoint recognition system! Facial keypoints include points around the eyes, nose, and mouth on any face and are used in many applications, from facial tracking to emotion recognition.

There are three main parts to this project:

Part 1 : Investigating OpenCV, pre-processing, and face detection

Part 2 : Training a Convolutional Neural Network (CNN) to detect facial keypoints

Part 3 : Putting parts 1 and 2 together to identify facial keypoints on any image!


*Here's what you need to know to complete the project:

  1. In this notebook, some template code has already been provided for you, and you will need to implement additional functionality to successfully complete this project. You will not need to modify the included code beyond what is requested.

    a. Sections that begin with '(IMPLEMENTATION)' in the header indicate that the following block of code will require additional functionality which you must provide. Instructions will be provided for each section, and the specifics of the implementation are marked in the code block with a 'TODO' statement. Please be sure to read the instructions carefully!

  1. In addition to implementing code, there will be questions that you must answer which relate to the project and your implementation.

    a. Each section where you will answer a question is preceded by a 'Question X' header.

    b. Carefully read each question and provide thorough answers in the following text boxes that begin with 'Answer:'.

Note: Code and Markdown cells can be executed using the Shift + Enter keyboard shortcut. Markdown cells can be edited by double-clicking the cell to enter edit mode.

The rubric contains optional suggestions for enhancing the project beyond the minimum requirements. If you decide to pursue the "(Optional)" sections, you should include the code in this IPython notebook.

Your project submission will be evaluated based on your answers to each of the questions and the code implementations you provide.

Steps to Complete the Project

Each part of the notebook is further broken down into separate steps. Feel free to use the links below to navigate the notebook.

In this project you will get to explore a few of the many computer vision algorithms built into the OpenCV library. This expansive computer vision library is now almost 20 years old and still growing!

The project itself is broken down into three large parts, then even further into separate steps. Make sure to read through each step, and complete any sections that begin with '(IMPLEMENTATION)' in the header; these implementation sections may contain multiple TODOs that will be marked in code. For convenience, we provide links to each of these steps below.

Part 1 : Investigating OpenCV, pre-processing, and face detection

  • Step 0: Detect Faces Using a Haar Cascade Classifier
  • Step 1: Add Eye Detection
  • Step 2: De-noise an Image for Better Face Detection
  • Step 3: Blur an Image and Perform Edge Detection
  • Step 4: Automatically Hide the Identity of an Individual

Part 2 : Training a Convolutional Neural Network (CNN) to detect facial keypoints

  • Step 5: Create a CNN to Recognize Facial Keypoints
  • Step 6: Compile and Train the Model
  • Step 7: Visualize the Loss and Answer Questions

Part 3 : Putting parts 1 and 2 together to identify facial keypoints on any image!

  • Step 8: Build a Robust Facial Keypoints Detector (Complete the CV Pipeline)

Step 0: Detect Faces Using a Haar Cascade Classifier

Have you ever wondered how Facebook automatically tags images with your friends' faces? Or how high-end cameras automatically find and focus on a certain person's face? Applications like these depend heavily on the machine learning task known as face detection - which is the task of automatically finding faces in images containing people.

At its root face detection is a classification problem - that is a problem of distinguishing between distinct classes of things. With face detection these distinct classes are 1) images of human faces and 2) everything else.

We use OpenCV's implementation of Haar feature-based cascade classifiers to detect human faces in images. OpenCV provides many pre-trained face detectors, stored as XML files on github. We have downloaded one of these detectors and stored it in the detector_architectures directory.

Import Resources

In the next python cell, we load in the required libraries for this section of the project.

In [1]:
# Import required libraries for this section

%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt
import math
import cv2                     # OpenCV library for computer vision
from PIL import Image
import time 

Next, we load in and display a test image for performing face detection.

Note: by default OpenCV assumes the ordering of our image's color channels are Blue, then Green, then Red. This is slightly out of order with most image types we'll use in these experiments, whose color channels are ordered Red, then Green, then Blue. In order to switch the Blue and Red channels of our test image around we will use OpenCV's cvtColor function, which you can read more about by checking out some of its documentation located here. This is a general utility function that can do other transformations too like converting a color image to grayscale, and transforming a standard color image to HSV color space.

In [2]:
# Load in color image for face detection
image = cv2.imread('images/test_image_1.jpg')

# Convert the image to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Plot our image using subplots to specify a size and title
fig = plt.figure(figsize = (8,8))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Original Image')
ax1.imshow(image)
Out[2]:
<matplotlib.image.AxesImage at 0x113a98630>

There are a lot of people - and faces - in this picture. 13 faces to be exact! In the next code cell, we demonstrate how to use a Haar Cascade classifier to detect all the faces in this test image.

This face detector uses information about patterns of intensity in an image to reliably detect faces under varying light conditions. So, to use this face detector, we'll first convert the image from color to grayscale.

Then, we load in the fully trained architecture of the face detector -- found in the file haarcascade_frontalface_default.xml - and use it on our image to find faces!

To learn more about the parameters of the detector see this post.

In [4]:
# Convert the RGB  image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

# Extract the pre-trained face detector from an xml file
face_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_frontalface_default.xml')

# Detect the faces in image
faces = face_cascade.detectMultiScale(gray, 4, 6)

# Print the number of faces detected in the image
print('Number of faces detected:', len(faces))

# Make a copy of the orginal image to draw face detections on
image_with_detections = np.copy(image)

# Get the bounding box for each detected face
for (x,y,w,h) in faces:
    # Add a red bounding box to the detections image
    cv2.rectangle(image_with_detections, (x,y), (x+w,y+h), (255,0,0), 3)
    

# Display the image with the detections
fig = plt.figure(figsize = (8,8))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Image with Face Detections')
ax1.imshow(image_with_detections)
Number of faces detected: 13
Out[4]:
<matplotlib.image.AxesImage at 0x113ec9a90>

In the above code, faces is a numpy array of detected faces, where each row corresponds to a detected face. Each detected face is a 1D array with four entries that specifies the bounding box of the detected face. The first two entries in the array (extracted in the above code as x and y) specify the horizontal and vertical positions of the top left corner of the bounding box. The last two entries in the array (extracted here as w and h) specify the width and height of the box.


Step 1: Add Eye Detections

There are other pre-trained detectors available that use a Haar Cascade Classifier - including full human body detectors, license plate detectors, and more. A full list of the pre-trained architectures can be found here.

To test your eye detector, we'll first read in a new test image with just a single face.

In [5]:
# Load in color image for face detection
image = cv2.imread('images/james.jpg')

# Convert the image to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Plot the RGB image
fig = plt.figure(figsize = (6,6))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Original Image')
ax1.imshow(image)
Out[5]:
<matplotlib.image.AxesImage at 0x11fdc69e8>

Notice that even though the image is a black and white image, we have read it in as a color image and so it will still need to be converted to grayscale in order to perform the most accurate face detection.

So, the next steps will be to convert this image to grayscale, then load OpenCV's face detector and run it with parameters that detect this face accurately.

In [6]:
# Convert the RGB  image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

# Extract the pre-trained face detector from an xml file
face_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_frontalface_default.xml')

# Detect the faces in image
faces = face_cascade.detectMultiScale(gray, 1.25, 6)

# Print the number of faces detected in the image
print('Number of faces detected:', len(faces))

# Make a copy of the orginal image to draw face detections on
image_with_detections = np.copy(image)

# Get the bounding box for each detected face
for (x,y,w,h) in faces:
    # Add a red bounding box to the detections image
    cv2.rectangle(image_with_detections, (x,y), (x+w,y+h), (255,0,0), 3)
    

# Display the image with the detections
fig = plt.figure(figsize = (6,6))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Image with Face Detection')
ax1.imshow(image_with_detections)
Number of faces detected: 1
Out[6]:
<matplotlib.image.AxesImage at 0x11fd23f28>

(IMPLEMENTATION) Add an eye detector to the current face detection setup.

A Haar-cascade eye detector can be included in the same way that the face detector was and, in this first task, it will be your job to do just this.

To set up an eye detector, use the stored parameters of the eye cascade detector, called haarcascade_eye.xml, located in the detector_architectures subdirectory. In the next code cell, create your eye detector and store its detections.

A few notes before you get started:

First, make sure to give your loaded eye detector the variable name

eye_cascade

and give the list of eye regions you detect the variable name

eyes

Second, since we've already run the face detector over this image, you should only search for eyes within the rectangular face regions detected in faces. This will minimize false detections.

Lastly, once you've run your eye detector over the facial detection region, you should display the RGB image with both the face detection boxes (in red) and your eye detections (in green) to verify that everything works as expected.

In [7]:
# Make a copy of the original image to plot rectangle detections
image_with_detections = np.copy(image)   

# Loop over the detections and draw their corresponding face detection boxes
for (x,y,w,h) in faces:
    cv2.rectangle(image_with_detections, (x,y), (x+w,y+h),(255,0,0), 3)  
    
# Do not change the code above this comment!

    
## TODO: Add eye detection, using haarcascade_eye.xml, to the current face detector algorithm
## TODO: Loop over the eye detections and draw their corresponding boxes in green on image_with_detections
eye_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_eye.xml')
eyes = eye_cascade.detectMultiScale(gray, 1.1, 6)

for (x, y, w, h) in eyes:
    cv2.rectangle(image_with_detections, (x, y), (x+w, y+h), (0, 0, 255), 3)

# Plot the image with both faces and eyes detected
fig = plt.figure(figsize = (6,6))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Image with Face and Eye Detection')
ax1.imshow(image_with_detections)
Out[7]:
<matplotlib.image.AxesImage at 0x12313eba8>

(Optional) Add face and eye detection to your laptop camera

It's time to kick it up a notch, and add face and eye detection to your laptop's camera! Afterwards, you'll be able to show off your creation like in the gif shown below - made with a completed version of the code!

Notice that not all of the detections here are perfect - and your result need not be perfect either. You should spend a small amount of time tuning the parameters of your detectors to get reasonable results, but don't hold out for perfection. If we wanted perfection we'd need to spend a ton of time tuning the parameters of each detector, cleaning up the input image frames, etc. You can think of this as more of a rapid prototype.

The next cell contains code for a wrapper function called laptop_camera_face_eye_detector that, when called, will activate your laptop's camera. You will place the relevant face and eye detection code in this wrapper function to implement face/eye detection and mark those detections on each image frame that your camera captures.

Before adding anything to the function, you can run it to get an idea of how it works - a small window should pop up showing you the live feed from your camera; you can press any key to close this window.

Note: Mac users may find that activating this function kills the kernel of their notebook every once in a while. If this happens to you, just restart your notebook's kernel, activate cell(s) containing any crucial import statements, and you'll be good to go!

In [8]:
### Add face and eye detection to this laptop camera function 
# Make sure to draw out all faces/eyes found in each frame on the shown video feed

import cv2
import time 

# wrapper function for face/eye detection with your laptop camera
def laptop_camera_go():
    # Create instance of video capturer
    cv2.namedWindow("face detection activated")
    vc = cv2.VideoCapture(0)

    # Try to get the first frame
    if vc.isOpened(): 
        rval, frame = vc.read()
    else:
        rval = False
    
    # Keep the video stream open
    while rval:
        gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
        
        face_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_frontalface_default.xml')
        eye_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_eye.xml')
        
        faces = face_cascade.detectMultiScale(gray, 1.1, 6)
        
        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 3)
            face = gray[y: y+h, x: x+w]
            eyes = eye_cascade.detectMultiScale(face, 1.05, 6)
            gray_face = gray[y: y+h, x: x+w]
            for (x1, y1, w1, h1) in eyes:
                cv2.rectangle(gray_face, (x1, y1), (x1+w1, y1+w1), (0, 255, 0), 3)
            
                
        # Plot the image from camera with all the face and eye detections marked
        cv2.imshow("face detection activated", frame)
        
        # Exit functionality - press any key to exit laptop video
        key = cv2.waitKey(20)
        if key > 0: # Exit by pressing any key
            # Destroy windows 
            cv2.destroyAllWindows()
            
            # Make sure window closes on OSx
            for i in range (1,5):
                cv2.waitKey(1)
            return
        
        # Read next frame
        time.sleep(0.05)             # control framerate for computation - default 20 frames per sec
        rval, frame = vc.read()    
In [25]:
# Call the laptop camera face/eye detector function above
laptop_camera_go()
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-25-6947bd6b284f> in <module>()
      1 # Call the laptop camera face/eye detector function above
----> 2 laptop_camera_go()

<ipython-input-24-94d68e89c81b> in laptop_camera_go()
     39 
     40         # Exit functionality - press any key to exit laptop video
---> 41         key = cv2.waitKey(20)
     42         if key > 0: # Exit by pressing any key
     43             # Destroy windows

KeyboardInterrupt: 

Step 2: De-noise an Image for Better Face Detection

Image quality is an important aspect of any computer vision task. Typically, when creating a set of images to train a deep learning network, significant care is taken to ensure that training images are free of visual noise or artifacts that hinder object detection. While computer vision algorithms - like a face detector - are typically trained on 'nice' data such as this, new test data doesn't always look so nice!

When applying a trained computer vision algorithm to a new piece of test data one often cleans it up first before feeding it in. This sort of cleaning - referred to as pre-processing - can include a number of cleaning phases like blurring, de-noising, color transformations, etc., and many of these tasks can be accomplished using OpenCV.

In this short subsection we explore OpenCV's noise-removal functionality to see how we can clean up a noisy image, which we then feed into our trained face detector.

Create a noisy image to work with

In the next cell, we create an artificial noisy version of the previous multi-face image. This is a little exaggerated - we don't typically get images that are this noisy - but image noise, or 'grainy-ness' in a digitial image - is a fairly common phenomenon.

In [9]:
# Load in the multi-face test image again
image = cv2.imread('images/test_image_1.jpg')

# Convert the image copy to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Make an array copy of this image
image_with_noise = np.asarray(image)

# Create noise - here we add noise sampled randomly from a Gaussian distribution: a common model for noise
noise_level = 40
noise = np.random.randn(image.shape[0],image.shape[1],image.shape[2])*noise_level

# Add this noise to the array image copy
image_with_noise = image_with_noise + noise

# Convert back to uint8 format
image_with_noise = np.asarray([np.uint8(np.clip(i,0,255)) for i in image_with_noise])

# Plot our noisy image!
fig = plt.figure(figsize = (8,8))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Noisy Image')
ax1.imshow(image_with_noise)
Out[9]:
<matplotlib.image.AxesImage at 0x113cdf0f0>

In the context of face detection, the problem with an image like this is that - due to noise - we may miss some faces or get false detections.

In the next cell we apply the same trained OpenCV detector with the same settings as before, to see what sort of detections we get.

In [10]:
# Convert the RGB  image to grayscale
gray_noise = cv2.cvtColor(image_with_noise, cv2.COLOR_RGB2GRAY)

# Extract the pre-trained face detector from an xml file
face_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_frontalface_default.xml')

# Detect the faces in image
faces = face_cascade.detectMultiScale(gray_noise, 4, 6)

# Print the number of faces detected in the image
print('Number of faces detected:', len(faces))

# Make a copy of the orginal image to draw face detections on
image_with_detections = np.copy(image_with_noise)

# Get the bounding box for each detected face
for (x,y,w,h) in faces:
    # Add a red bounding box to the detections image
    cv2.rectangle(image_with_detections, (x,y), (x+w,y+h), (255,0,0), 3)
    

# Display the image with the detections
fig = plt.figure(figsize = (8,8))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Noisy Image with Face Detections')
ax1.imshow(image_with_detections)
Number of faces detected: 12
Out[10]:
<matplotlib.image.AxesImage at 0x113cc4cc0>

With this added noise we now miss one of the faces!

(IMPLEMENTATION) De-noise this image for better face detection

Time to get your hands dirty: using OpenCV's built in color image de-noising functionality called fastNlMeansDenoisingColored - de-noise this image enough so that all the faces in the image are properly detected. Once you have cleaned the image in the next cell, use the cell that follows to run our trained face detector over the cleaned image to check out its detections.

You can find its official documentation here and a useful example here.

Note: you can keep all parameters except photo_render fixed as shown in the second link above. Play around with the value of this parameter - see how it affects the resulting cleaned image.

In [11]:
## TODO: Use OpenCV's built in color image de-noising function to clean up our noisy image!

denoised_image = cv2.fastNlMeansDenoisingColored(image_with_noise,None,20,20,7,21)
In [12]:
def show(image, cmap=None, title=''):
    figure = plt.figure(figsize = (8, 8))
    ax1 = figure.add_subplot(111)
    ax1.set_xticks([])
    ax1.set_yticks([])
    
    ax1.set_title(title)
    ax1.imshow(image, cmap=cmap)
In [13]:
show(image_with_noise, title='Noisy Image')
show(denoised_image, title='Denoised Image')
In [14]:
## TODO: Run the face detector on the de-noised image to improve your detections and display the result

def face_detector(image, scaleFactor=1.1, minNeighbors=6):
    gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    
    face_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_frontalface_default.xml')
    
    faces = face_cascade.detectMultiScale(gray, scaleFactor, minNeighbors)
    
    print('Number of faces detected: ', len(faces))
    
    return faces
In [15]:
def plot_faces(image, faces):
    for (x, y, w, h) in faces:
        cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 3)
        
    return
In [16]:
denoised_image_with_detections = np.copy(denoised_image)

faces = face_detector(denoised_image_with_detections, 4, 6)

plot_faces(denoised_image_with_detections, faces)

show(denoised_image_with_detections, title='Denoised Image with Face Detection')
Number of faces detected:  12

Step 3: Blur an Image and Perform Edge Detection

Now that we have developed a simple pipeline for detecting faces using OpenCV - let's start playing around with a few fun things we can do with all those detected faces!

Importance of Blur in Edge Detection

Edge detection is a concept that pops up almost everywhere in computer vision applications, as edge-based features (as well as features built on top of edges) are often some of the best features for e.g., object detection and recognition problems.

Edge detection is a dimension reduction technique - by keeping only the edges of an image we get to throw away a lot of non-discriminating information. And typically the most useful kind of edge-detection is one that preserves only the important, global structures (ignoring local structures that aren't very discriminative). So removing local structures / retaining global structures is a crucial pre-processing step to performing edge detection in an image, and blurring can do just that.

Below is an animated gif showing the result of an edge-detected cat taken from Wikipedia, where the image is gradually blurred more and more prior to edge detection. When the animation begins you can't quite make out what it's a picture of, but as the animation evolves and local structures are removed via blurring the cat becomes visible in the edge-detected image.

Edge detection is a convolution performed on the image itself, and you can read about Canny edge detection on this OpenCV documentation page.

Canny edge detection

In the cell below we load in a test image, then apply Canny edge detection on it. The original image is shown on the left panel of the figure, while the edge-detected version of the image is shown on the right. Notice how the result looks very busy - there are too many little details preserved in the image before it is sent to the edge detector. When applied in computer vision applications, edge detection should preserve global structure; doing away with local structures that don't help describe what objects are in the image.

In [17]:
# Load in the image
image = cv2.imread('images/fawzia.jpg')

# Convert to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)  

# Perform Canny edge detection
edges = cv2.Canny(gray,100,200)

# Dilate the image to amplify edges
edges = cv2.dilate(edges, None)

# Plot the RGB and edge-detected image
fig = plt.figure(figsize = (15,15))
ax1 = fig.add_subplot(121)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Original Image')
ax1.imshow(image)

ax2 = fig.add_subplot(122)
ax2.set_xticks([])
ax2.set_yticks([])

ax2.set_title('Canny Edges')
ax2.imshow(edges, cmap='gray')
Out[17]:
<matplotlib.image.AxesImage at 0x12b7e5c88>

Without first blurring the image, and removing small, local structures, a lot of irrelevant edge content gets picked up and amplified by the detector (as shown in the right panel above).

(IMPLEMENTATION) Blur the image then perform edge detection

In the next cell, you will repeat this experiment - blurring the image first to remove these local structures, so that only the important boudnary details remain in the edge-detected image.

Blur the image by using OpenCV's filter2d functionality - which is discussed in this documentation page - and use an averaging kernel of width equal to 4.

In [18]:
### TODO: Blur the test imageusing OpenCV's filter2d functionality, 
# Use an averaging kernel, and a kernel width equal to 4

kernel = np.ones((4, 4), np.float32)/16

filtered_gray = cv2.filter2D(gray, -1, kernel)

show(filtered_gray, title='Filtered Gray Image', cmap='gray')
## TODO: Then perform Canny edge detection and display the output
edges = cv2.Canny(filtered_gray, 100, 200)

edges = cv2.dilate(edges, None)

show(edges, title='Canny Edges', cmap='gray')

Step 4: Automatically Hide the Identity of an Individual

If you film something like a documentary or reality TV, you must get permission from every individual shown on film before you can show their face, otherwise you need to blur it out - by blurring the face a lot (so much so that even the global structures are obscured)! This is also true for projects like Google's StreetView maps - an enormous collection of mapping images taken from a fleet of Google vehicles. Because it would be impossible for Google to get the permission of every single person accidentally captured in one of these images they blur out everyone's faces, the detected images must automatically blur the identity of detected people. Here's a few examples of folks caught in the camera of a Google street view vehicle.

Read in an image to perform identity detection

Let's try this out for ourselves. Use the face detection pipeline built above and what you know about using the filter2D to blur and image, and use these in tandem to hide the identity of the person in the following image - loaded in and printed in the next cell.

In [19]:
# Load in the image
image = cv2.imread('images/gus.jpg')

# Convert the image to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Display the image
fig = plt.figure(figsize = (6,6))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])
ax1.set_title('Original Image')
ax1.imshow(image)
Out[19]:
<matplotlib.image.AxesImage at 0x1229f2c18>

(IMPLEMENTATION) Use blurring to hide the identity of an individual in an image

The idea here is to 1) automatically detect the face in this image, and then 2) blur it out! Make sure to adjust the parameters of the averaging blur filter to completely obscure this person's identity.

In [20]:
## TODO: Implement face detection

image_with_detections = np.copy(image)

face_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_frontalface_default.xml')

faces = face_cascade.detectMultiScale(image_with_detections, 1.3, 6)

## TODO: Blur the bounding box around each detected face using an averaging filter and display the result

image_with_blurred_face = np.copy(image_with_detections)
kernel_size = 100
kernel = np.ones((kernel_size, kernel_size), np.float32)/(kernel_size**2)
for (x, y, w, h) in faces:
    blurred_area = image_with_blurred_face[y: y+h, x: x+w]
    image_with_blurred_face[y: y+h, x: x+w] = cv2.filter2D(blurred_area, -1, kernel)

show(image_with_blurred_face)

(Optional) Build identity protection into your laptop camera

In this optional task you can add identity protection to your laptop camera, using the previously completed code where you added face detection to your laptop camera - and the task above. You should be able to get reasonable results with little parameter tuning - like the one shown in the gif below.

As with the previous video task, to make this perfect would require significant effort - so don't strive for perfection here, strive for reasonable quality.

The next cell contains code a wrapper function called laptop_camera_identity_hider that - when called - will activate your laptop's camera. You need to place the relevant face detection and blurring code developed above in this function in order to blur faces entering your laptop camera's field of view.

Before adding anything to the function you can call it to get a hang of how it works - a small window will pop up showing you the live feed from your camera, you can press any key to close this window.

Note: Mac users may find that activating this function kills the kernel of their notebook every once in a while. If this happens to you, just restart your notebook's kernel, activate cell(s) containing any crucial import statements, and you'll be good to go!

In [21]:
### Insert face detection and blurring code into the wrapper below to create an identity protector on your laptop!
import cv2
import time 

def laptop_camera_go():
    # Create instance of video capturer
    cv2.namedWindow("face detection activated")
    vc = cv2.VideoCapture(0)

    # Try to get the first frame
    if vc.isOpened(): 
        rval, frame = vc.read()
    else:
        rval = False
    
    # Keep video stream open
    while rval:
        gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
        
        face_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_frontalface_default.xml')
        
        faces = face_cascade.detectMultiScale(gray, 1.1, 5)
        
        for (x, y, w, h) in faces:
            frame[y: y+h, x: x+w] = cv2.GaussianBlur(frame[y: y+h, x: x+w], (101, 101), 100)
        # Plot image from camera with detections marked
        cv2.imshow("face detection activated", frame)
        
        # Exit functionality - press any key to exit laptop video
        key = cv2.waitKey(20)
        if key > 0: # Exit by pressing any key
            # Destroy windows
            cv2.destroyAllWindows()
            
            for i in range (1,5):
                cv2.waitKey(1)
            return
        
        # Read next frame
        time.sleep(0.05)             # control framerate for computation - default 20 frames per sec
        rval, frame = vc.read()    
        
In [73]:
# Run laptop identity hider
laptop_camera_go()
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-73-980123c929f8> in <module>()
      1 # Run laptop identity hider
----> 2 laptop_camera_go()

<ipython-input-72-afbabc119e3e> in laptop_camera_go()
     26 
     27         # Exit functionality - press any key to exit laptop video
---> 28         key = cv2.waitKey(20)
     29         if key > 0: # Exit by pressing any key
     30             # Destroy windows

KeyboardInterrupt: 

Step 5: Create a CNN to Recognize Facial Keypoints

OpenCV is often used in practice with other machine learning and deep learning libraries to produce interesting results. In this stage of the project you will create your own end-to-end pipeline - employing convolutional networks in keras along with OpenCV - to apply a "selfie" filter to streaming video and images.

You will start by creating and then training a convolutional network that can detect facial keypoints in a small dataset of cropped images of human faces. We then guide you towards OpenCV to expanding your detection algorithm to more general images. What are facial keypoints? Let's take a look at some examples.

Facial keypoints (also called facial landmarks) are the small blue-green dots shown on each of the faces in the image above - there are 15 keypoints marked in each image. They mark important areas of the face - the eyes, corners of the mouth, the nose, etc. Facial keypoints can be used in a variety of machine learning applications from face and emotion recognition to commercial applications like the image filters popularized by Snapchat.

Below we illustrate a filter that, using the results of this section, automatically places sunglasses on people in images (using the facial keypoints to place the glasses correctly on each face). Here, the facial keypoints have been colored lime green for visualization purposes.

Make a facial keypoint detector

But first things first: how can we make a facial keypoint detector? Well, at a high level, notice that facial keypoint detection is a regression problem. A single face corresponds to a set of 15 facial keypoints (a set of 15 corresponding $(x, y)$ coordinates, i.e., an output point). Because our input data are images, we can employ a convolutional neural network to recognize patterns in our images and learn how to identify these keypoint given sets of labeled data.

In order to train a regressor, we need a training set - a set of facial image / facial keypoint pairs to train on. For this we will be using this dataset from Kaggle. We've already downloaded this data and placed it in the data directory. Make sure that you have both the training and test data files. The training dataset contains several thousand $96 \times 96$ grayscale images of cropped human faces, along with each face's 15 corresponding facial keypoints (also called landmarks) that have been placed by hand, and recorded in $(x, y)$ coordinates. This wonderful resource also has a substantial testing set, which we will use in tinkering with our convolutional network.

To load in this data, run the Python cell below - notice we will load in both the training and testing sets.

The load_data function is in the included utils.py file.

In [22]:
from utils import *

# Load training set
X_train, y_train = load_data()
print("X_train.shape == {}".format(X_train.shape))
print("y_train.shape == {}; y_train.min == {:.3f}; y_train.max == {:.3f}".format(
    y_train.shape, y_train.min(), y_train.max()))

# Load testing set
X_test, _ = load_data(test=True)
print("X_test.shape == {}".format(X_test.shape))
Using TensorFlow backend.
X_train.shape == (2140, 96, 96, 1)
y_train.shape == (2140, 30); y_train.min == -0.920; y_train.max == 0.996
X_test.shape == (1783, 96, 96, 1)

The load_data function in utils.py originates from this excellent blog post, which you are strongly encouraged to read. Please take the time now to review this function. Note how the output values - that is, the coordinates of each set of facial landmarks - have been normalized to take on values in the range $[-1, 1]$, while the pixel values of each input point (a facial image) have been normalized to the range $[0,1]$.

Note: the original Kaggle dataset contains some images with several missing keypoints. For simplicity, the load_data function removes those images with missing labels from the dataset. As an optional extension, you are welcome to amend the load_data function to include the incomplete data points.

Visualize the Training Data

Execute the code cell below to visualize a subset of the training data.

In [21]:
import matplotlib.pyplot as plt
%matplotlib inline

fig = plt.figure(figsize=(20,20))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)
for i in range(9):
    ax = fig.add_subplot(3, 3, i + 1, xticks=[], yticks=[])
    plot_data(X_train[i], y_train[i], ax)

For each training image, there are two landmarks per eyebrow (four total), three per eye (six total), four for the mouth, and one for the tip of the nose.

Review the plot_data function in utils.py to understand how the 30-dimensional training labels in y_train are mapped to facial locations, as this function will prove useful for your pipeline.

(IMPLEMENTATION) Specify the CNN Architecture

In this section, you will specify a neural network for predicting the locations of facial keypoints. Use the code cell below to specify the architecture of your neural network. We have imported some layers that you may find useful for this task, but if you need to use more Keras layers, feel free to import them in the cell.

Your network should accept a $96 \times 96$ grayscale image as input, and it should output a vector with 30 entries, corresponding to the predicted (horizontal and vertical) locations of 15 facial keypoints. If you are not sure where to start, you can find some useful starting architectures in this blog, but you are not permitted to copy any of the architectures that you find online.

In [23]:
# Import deep learning resources from Keras
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dropout
from keras.layers import Flatten, Dense


## TODO: Specify a CNN architecture
# Your model should accept 96x96 pixel graysale images in
# It should have a fully-connected output layer with 30 values (2 for each facial keypoint)

def create_model():
    model = Sequential()
    model.add(Conv2D(16, 3, padding='same', activation='relu', input_shape=(96, 96, 1)))
    model.add(MaxPooling2D(pool_size=2))
    model.add(Conv2D(32, 3, padding='same', activation='relu'))
    model.add(MaxPooling2D(pool_size=2))
    model.add(Conv2D(64, 3, padding='same', activation='relu'))
    model.add(MaxPooling2D(pool_size=2))

    model.add(Flatten())
    model.add(Dropout(0.5))
    model.add(Dense(500, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(30, activation='tanh'))
    
    return model

model = create_model()

# Summarize the model
model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 96, 96, 16)        160       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 48, 48, 16)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 48, 48, 32)        4640      
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 24, 24, 32)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 24, 24, 64)        18496     
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 12, 12, 64)        0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 9216)              0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 9216)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 500)               4608500   
_________________________________________________________________
dropout_2 (Dropout)          (None, 500)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 30)                15030     
=================================================================
Total params: 4,646,826
Trainable params: 4,646,826
Non-trainable params: 0
_________________________________________________________________

Step 6: Compile and Train the Model

After specifying your architecture, you'll need to compile and train the model to detect facial keypoints'

(IMPLEMENTATION) Compile and Train the Model

Use the compile method to configure the learning process. Experiment with your choice of optimizer; you may have some ideas about which will work best (SGD vs. RMSprop, etc), but take the time to empirically verify your theories.

Use the fit method to train the model. Break off a validation set by setting validation_split=0.2. Save the returned History object in the history variable.

Your model is required to attain a validation loss (measured as mean squared error) of at least XYZ. When you have finished training, save your model as an HDF5 file with file path my_model.h5.

In [26]:
from keras.callbacks import ModelCheckpoint
from keras.optimizers import SGD, RMSprop, Adagrad, Adadelta, Adam, Adamax, Nadam

def create_and_compile_model(optimizer):
    model = create_model()
    model.compile(loss='mean_squared_error', optimizer=optimizer, metrics=['accuracy'])
    return model

def train(model, batch_size=32, epochs=30, validation_split=0.2, checkpoint_path=None):
    if checkpoint_path == None:
        return model.fit(X_train, y_train, batch_size = batch_size, epochs=epochs, 
                         validation_split=validation_split,
                         verbose=1, shuffle=True)
    
    checkpointer = ModelCheckpoint(checkpoint_path,
                                   verbose=1, save_best_only=True)
    hist = model.fit(X_train, y_train, batch_size = batch_size, epochs=epochs, 
                     callbacks=[checkpointer], validation_split=validation_split,
                     verbose=1, shuffle=True)
    return hist

path = 'saved_model/best_model_with_{}.hdf5'
In [29]:
## TODO: Compile the model

model_sgd = create_and_compile_model('sgd')
model_rmsprop = create_and_compile_model('rmsprop')
model_adagrad = create_and_compile_model('adagrad')
model_adadelta = create_and_compile_model('adadelta')
model_adam = create_and_compile_model('adam')
model_adamax = create_and_compile_model('adamax')
model_nadam = create_and_compile_model('nadam')


## TODO: Train the model
# I've moved it to the previous cell so that following cells could run successfully without running this cell

hist_sgd = train(model_sgd, checkpoint_path=path.format('sgd'))
hist_rmsprop = train(model_rmsprop, checkpoint_path=path.format('rmsprop'))
hist_adagrad = train(model_adagrad, checkpoint_path=path.format('adagrad'))
hist_adadelta = train(model_adadelta, checkpoint_path=path.format('adadelta'))
hist_adam = train(model_adam, checkpoint_path=path.format('adam'))
hist_adamax = train(model_adamax, checkpoint_path=path.format('adamax'))
hist_nadam = train(model_nadam, checkpoint_path=path.format('nadam'))
## TODO: Save the model as model.h5
# Nothing here, models have already been saved before 
Train on 1712 samples, validate on 428 samples
Epoch 1/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.1264 - acc: 0.1303Epoch 00000: val_loss improved from inf to 0.07326, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 17s - loss: 0.1261 - acc: 0.1308 - val_loss: 0.0733 - val_acc: 0.6893
Epoch 2/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0618 - acc: 0.2429Epoch 00001: val_loss improved from 0.07326 to 0.01860, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 14s - loss: 0.0617 - acc: 0.2418 - val_loss: 0.0186 - val_acc: 0.6963
Epoch 3/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0394 - acc: 0.2901Epoch 00002: val_loss improved from 0.01860 to 0.01149, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 14s - loss: 0.0394 - acc: 0.2903 - val_loss: 0.0115 - val_acc: 0.6963
Epoch 4/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0328 - acc: 0.3172Epoch 00003: val_loss improved from 0.01149 to 0.00976, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 14s - loss: 0.0328 - acc: 0.3166 - val_loss: 0.0098 - val_acc: 0.6963
Epoch 5/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0294 - acc: 0.3502Epoch 00004: val_loss improved from 0.00976 to 0.00866, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 13s - loss: 0.0294 - acc: 0.3487 - val_loss: 0.0087 - val_acc: 0.6963
Epoch 6/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0267 - acc: 0.3620Epoch 00005: val_loss improved from 0.00866 to 0.00834, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 13s - loss: 0.0268 - acc: 0.3627 - val_loss: 0.0083 - val_acc: 0.6963
Epoch 7/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0251 - acc: 0.3868Epoch 00006: val_loss improved from 0.00834 to 0.00792, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 12s - loss: 0.0251 - acc: 0.3861 - val_loss: 0.0079 - val_acc: 0.6963
Epoch 8/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0237 - acc: 0.4045Epoch 00007: val_loss improved from 0.00792 to 0.00740, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 12s - loss: 0.0237 - acc: 0.4042 - val_loss: 0.0074 - val_acc: 0.6963
Epoch 9/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0227 - acc: 0.4192Epoch 00008: val_loss improved from 0.00740 to 0.00716, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 13s - loss: 0.0227 - acc: 0.4211 - val_loss: 0.0072 - val_acc: 0.6963
Epoch 10/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0215 - acc: 0.4281Epoch 00009: val_loss improved from 0.00716 to 0.00711, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 13s - loss: 0.0215 - acc: 0.4293 - val_loss: 0.0071 - val_acc: 0.6963
Epoch 11/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0205 - acc: 0.4263Epoch 00010: val_loss improved from 0.00711 to 0.00692, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 13s - loss: 0.0205 - acc: 0.4270 - val_loss: 0.0069 - val_acc: 0.6963
Epoch 12/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0198 - acc: 0.4404Epoch 00011: val_loss improved from 0.00692 to 0.00649, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 13s - loss: 0.0198 - acc: 0.4398 - val_loss: 0.0065 - val_acc: 0.6963
Epoch 13/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0192 - acc: 0.4463Epoch 00012: val_loss improved from 0.00649 to 0.00649, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 16s - loss: 0.0192 - acc: 0.4457 - val_loss: 0.0065 - val_acc: 0.6963
Epoch 14/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0186 - acc: 0.4269Epoch 00013: val_loss did not improve
1712/1712 [==============================] - 14s - loss: 0.0186 - acc: 0.4282 - val_loss: 0.0067 - val_acc: 0.6963
Epoch 15/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0182 - acc: 0.4428Epoch 00014: val_loss improved from 0.00649 to 0.00625, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 13s - loss: 0.0182 - acc: 0.4433 - val_loss: 0.0063 - val_acc: 0.6963
Epoch 16/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0180 - acc: 0.4564Epoch 00015: val_loss improved from 0.00625 to 0.00609, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 13s - loss: 0.0180 - acc: 0.4562 - val_loss: 0.0061 - val_acc: 0.6963
Epoch 17/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0172 - acc: 0.4658Epoch 00016: val_loss improved from 0.00609 to 0.00606, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 12s - loss: 0.0172 - acc: 0.4655 - val_loss: 0.0061 - val_acc: 0.6963
Epoch 18/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0169 - acc: 0.5024Epoch 00017: val_loss did not improve
1712/1712 [==============================] - 12s - loss: 0.0169 - acc: 0.5023 - val_loss: 0.0061 - val_acc: 0.6963
Epoch 19/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0165 - acc: 0.4587Epoch 00018: val_loss improved from 0.00606 to 0.00599, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 12s - loss: 0.0165 - acc: 0.4579 - val_loss: 0.0060 - val_acc: 0.6963
Epoch 20/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0161 - acc: 0.4729Epoch 00019: val_loss improved from 0.00599 to 0.00581, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 12s - loss: 0.0160 - acc: 0.4737 - val_loss: 0.0058 - val_acc: 0.6963
Epoch 21/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0157 - acc: 0.5177Epoch 00020: val_loss improved from 0.00581 to 0.00574, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 13s - loss: 0.0157 - acc: 0.5164 - val_loss: 0.0057 - val_acc: 0.6963
Epoch 22/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0155 - acc: 0.4906Epoch 00021: val_loss improved from 0.00574 to 0.00572, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 13s - loss: 0.0155 - acc: 0.4907 - val_loss: 0.0057 - val_acc: 0.6963
Epoch 23/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0153 - acc: 0.5071Epoch 00022: val_loss improved from 0.00572 to 0.00569, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 13s - loss: 0.0153 - acc: 0.5053 - val_loss: 0.0057 - val_acc: 0.6963
Epoch 24/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0148 - acc: 0.4982Epoch 00023: val_loss improved from 0.00569 to 0.00560, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 12s - loss: 0.0148 - acc: 0.4982 - val_loss: 0.0056 - val_acc: 0.6963
Epoch 25/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0148 - acc: 0.5077Epoch 00024: val_loss improved from 0.00560 to 0.00559, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 13s - loss: 0.0148 - acc: 0.5064 - val_loss: 0.0056 - val_acc: 0.6963
Epoch 26/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0144 - acc: 0.5130Epoch 00025: val_loss improved from 0.00559 to 0.00540, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 13s - loss: 0.0144 - acc: 0.5134 - val_loss: 0.0054 - val_acc: 0.6963
Epoch 27/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0143 - acc: 0.5112Epoch 00026: val_loss did not improve
1712/1712 [==============================] - 12s - loss: 0.0143 - acc: 0.5123 - val_loss: 0.0055 - val_acc: 0.6963
Epoch 28/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0140 - acc: 0.5071Epoch 00027: val_loss improved from 0.00540 to 0.00539, saving model to saved_model/best_model_with_sgd.hdf5
1712/1712 [==============================] - 13s - loss: 0.0140 - acc: 0.5070 - val_loss: 0.0054 - val_acc: 0.6963
Epoch 29/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0140 - acc: 0.5088Epoch 00028: val_loss did not improve
1712/1712 [==============================] - 13s - loss: 0.0140 - acc: 0.5093 - val_loss: 0.0056 - val_acc: 0.6963
Epoch 30/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0137 - acc: 0.5307Epoch 00029: val_loss did not improve
1712/1712 [==============================] - 12s - loss: 0.0137 - acc: 0.5315 - val_loss: 0.0055 - val_acc: 0.6963
Train on 1712 samples, validate on 428 samples
Epoch 1/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0279 - acc: 0.5265Epoch 00000: val_loss improved from inf to 0.00553, saving model to saved_model/best_model_with_rmsprop.hdf5
1712/1712 [==============================] - 14s - loss: 0.0278 - acc: 0.5292 - val_loss: 0.0055 - val_acc: 0.6963
Epoch 2/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0109 - acc: 0.6509Epoch 00001: val_loss did not improve
1712/1712 [==============================] - 14s - loss: 0.0109 - acc: 0.6513 - val_loss: 0.0061 - val_acc: 0.6963
Epoch 3/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0079 - acc: 0.6857Epoch 00002: val_loss improved from 0.00553 to 0.00467, saving model to saved_model/best_model_with_rmsprop.hdf5
1712/1712 [==============================] - 13s - loss: 0.0079 - acc: 0.6875 - val_loss: 0.0047 - val_acc: 0.6963
Epoch 4/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0064 - acc: 0.6822Epoch 00003: val_loss improved from 0.00467 to 0.00365, saving model to saved_model/best_model_with_rmsprop.hdf5
1712/1712 [==============================] - 14s - loss: 0.0064 - acc: 0.6834 - val_loss: 0.0036 - val_acc: 0.6963
Epoch 5/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0054 - acc: 0.6975Epoch 00004: val_loss did not improve
1712/1712 [==============================] - 13s - loss: 0.0054 - acc: 0.6998 - val_loss: 0.0046 - val_acc: 0.6963
Epoch 6/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0047 - acc: 0.7070Epoch 00005: val_loss improved from 0.00365 to 0.00276, saving model to saved_model/best_model_with_rmsprop.hdf5
1712/1712 [==============================] - 13s - loss: 0.0047 - acc: 0.7074 - val_loss: 0.0028 - val_acc: 0.7033
Epoch 7/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0039 - acc: 0.7093Epoch 00006: val_loss did not improve
1712/1712 [==============================] - 13s - loss: 0.0039 - acc: 0.7103 - val_loss: 0.0034 - val_acc: 0.7009
Epoch 8/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0034 - acc: 0.7081Epoch 00007: val_loss did not improve
1712/1712 [==============================] - 13s - loss: 0.0034 - acc: 0.7097 - val_loss: 0.0028 - val_acc: 0.7079
Epoch 9/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0031 - acc: 0.7176Epoch 00008: val_loss improved from 0.00276 to 0.00254, saving model to saved_model/best_model_with_rmsprop.hdf5
1712/1712 [==============================] - 13s - loss: 0.0031 - acc: 0.7173 - val_loss: 0.0025 - val_acc: 0.6986
Epoch 10/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0029 - acc: 0.7182Epoch 00009: val_loss improved from 0.00254 to 0.00198, saving model to saved_model/best_model_with_rmsprop.hdf5
1712/1712 [==============================] - 13s - loss: 0.0029 - acc: 0.7185 - val_loss: 0.0020 - val_acc: 0.7103
Epoch 11/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0027 - acc: 0.7246Epoch 00010: val_loss improved from 0.00198 to 0.00180, saving model to saved_model/best_model_with_rmsprop.hdf5
1712/1712 [==============================] - 14s - loss: 0.0027 - acc: 0.7225 - val_loss: 0.0018 - val_acc: 0.7220
Epoch 12/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0026 - acc: 0.7205Epoch 00011: val_loss improved from 0.00180 to 0.00173, saving model to saved_model/best_model_with_rmsprop.hdf5
1712/1712 [==============================] - 14s - loss: 0.0026 - acc: 0.7214 - val_loss: 0.0017 - val_acc: 0.7220
Epoch 13/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0024 - acc: 0.7229Epoch 00012: val_loss improved from 0.00173 to 0.00165, saving model to saved_model/best_model_with_rmsprop.hdf5
1712/1712 [==============================] - 15s - loss: 0.0024 - acc: 0.7255 - val_loss: 0.0016 - val_acc: 0.7266
Epoch 14/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0024 - acc: 0.7258Epoch 00013: val_loss did not improve
1712/1712 [==============================] - 14s - loss: 0.0024 - acc: 0.7255 - val_loss: 0.0024 - val_acc: 0.6986
Epoch 15/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0022 - acc: 0.7288Epoch 00014: val_loss did not improve
1712/1712 [==============================] - 13s - loss: 0.0022 - acc: 0.7284 - val_loss: 0.0017 - val_acc: 0.7290
Epoch 16/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0021 - acc: 0.7382Epoch 00015: val_loss improved from 0.00165 to 0.00147, saving model to saved_model/best_model_with_rmsprop.hdf5
1712/1712 [==============================] - 13s - loss: 0.0022 - acc: 0.7395 - val_loss: 0.0015 - val_acc: 0.7383
Epoch 17/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0020 - acc: 0.7535Epoch 00016: val_loss did not improve
1712/1712 [==============================] - 13s - loss: 0.0020 - acc: 0.7535 - val_loss: 0.0017 - val_acc: 0.7570
Epoch 18/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0019 - acc: 0.7441Epoch 00017: val_loss did not improve
1712/1712 [==============================] - 13s - loss: 0.0019 - acc: 0.7430 - val_loss: 0.0015 - val_acc: 0.7407
Epoch 19/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0019 - acc: 0.7571Epoch 00018: val_loss improved from 0.00147 to 0.00140, saving model to saved_model/best_model_with_rmsprop.hdf5
1712/1712 [==============================] - 13s - loss: 0.0019 - acc: 0.7564 - val_loss: 0.0014 - val_acc: 0.7383
Epoch 20/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0018 - acc: 0.7506Epoch 00019: val_loss improved from 0.00140 to 0.00136, saving model to saved_model/best_model_with_rmsprop.hdf5
1712/1712 [==============================] - 14s - loss: 0.0018 - acc: 0.7506 - val_loss: 0.0014 - val_acc: 0.7664
Epoch 21/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0018 - acc: 0.7594Epoch 00020: val_loss improved from 0.00136 to 0.00129, saving model to saved_model/best_model_with_rmsprop.hdf5
1712/1712 [==============================] - 14s - loss: 0.0018 - acc: 0.7599 - val_loss: 0.0013 - val_acc: 0.7687
Epoch 22/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0017 - acc: 0.7642Epoch 00021: val_loss improved from 0.00129 to 0.00129, saving model to saved_model/best_model_with_rmsprop.hdf5
1712/1712 [==============================] - 15s - loss: 0.0017 - acc: 0.7617 - val_loss: 0.0013 - val_acc: 0.7734
Epoch 23/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0017 - acc: 0.7671Epoch 00022: val_loss improved from 0.00129 to 0.00127, saving model to saved_model/best_model_with_rmsprop.hdf5
1712/1712 [==============================] - 15s - loss: 0.0017 - acc: 0.7658 - val_loss: 0.0013 - val_acc: 0.7477
Epoch 24/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0016 - acc: 0.7706Epoch 00023: val_loss improved from 0.00127 to 0.00124, saving model to saved_model/best_model_with_rmsprop.hdf5
1712/1712 [==============================] - 15s - loss: 0.0016 - acc: 0.7704 - val_loss: 0.0012 - val_acc: 0.7430
Epoch 25/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0015 - acc: 0.7759Epoch 00024: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0015 - acc: 0.7763 - val_loss: 0.0017 - val_acc: 0.7407
Epoch 26/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0015 - acc: 0.7907Epoch 00025: val_loss improved from 0.00124 to 0.00115, saving model to saved_model/best_model_with_rmsprop.hdf5
1712/1712 [==============================] - 14s - loss: 0.0015 - acc: 0.7909 - val_loss: 0.0012 - val_acc: 0.7734
Epoch 27/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0014 - acc: 0.7930Epoch 00026: val_loss did not improve
1712/1712 [==============================] - 14s - loss: 0.0014 - acc: 0.7932 - val_loss: 0.0014 - val_acc: 0.7850
Epoch 28/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0014 - acc: 0.7889Epoch 00027: val_loss did not improve
1712/1712 [==============================] - 13s - loss: 0.0014 - acc: 0.7891 - val_loss: 0.0012 - val_acc: 0.7804
Epoch 29/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0014 - acc: 0.7812Epoch 00028: val_loss improved from 0.00115 to 0.00114, saving model to saved_model/best_model_with_rmsprop.hdf5
1712/1712 [==============================] - 13s - loss: 0.0014 - acc: 0.7804 - val_loss: 0.0011 - val_acc: 0.7944
Epoch 30/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0014 - acc: 0.7901Epoch 00029: val_loss improved from 0.00114 to 0.00112, saving model to saved_model/best_model_with_rmsprop.hdf5
1712/1712 [==============================] - 14s - loss: 0.0014 - acc: 0.7886 - val_loss: 0.0011 - val_acc: 0.7804
Train on 1712 samples, validate on 428 samples
Epoch 1/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0480 - acc: 0.5825Epoch 00000: val_loss improved from inf to 0.00492, saving model to saved_model/best_model_with_adagrad.hdf5
1712/1712 [==============================] - 14s - loss: 0.0476 - acc: 0.5829 - val_loss: 0.0049 - val_acc: 0.6963
Epoch 2/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0072 - acc: 0.6686Epoch 00001: val_loss improved from 0.00492 to 0.00418, saving model to saved_model/best_model_with_adagrad.hdf5
1712/1712 [==============================] - 13s - loss: 0.0072 - acc: 0.6694 - val_loss: 0.0042 - val_acc: 0.6963
Epoch 3/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0064 - acc: 0.6881Epoch 00002: val_loss improved from 0.00418 to 0.00392, saving model to saved_model/best_model_with_adagrad.hdf5
1712/1712 [==============================] - 13s - loss: 0.0064 - acc: 0.6875 - val_loss: 0.0039 - val_acc: 0.6963
Epoch 4/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0059 - acc: 0.6840Epoch 00003: val_loss improved from 0.00392 to 0.00373, saving model to saved_model/best_model_with_adagrad.hdf5
1712/1712 [==============================] - 13s - loss: 0.0059 - acc: 0.6834 - val_loss: 0.0037 - val_acc: 0.6963
Epoch 5/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0055 - acc: 0.6922Epoch 00004: val_loss improved from 0.00373 to 0.00366, saving model to saved_model/best_model_with_adagrad.hdf5
1712/1712 [==============================] - 13s - loss: 0.0055 - acc: 0.6939 - val_loss: 0.0037 - val_acc: 0.6963
Epoch 6/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0052 - acc: 0.7034Epoch 00005: val_loss did not improve
1712/1712 [==============================] - 13s - loss: 0.0052 - acc: 0.7021 - val_loss: 0.0039 - val_acc: 0.6963
Epoch 7/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0049 - acc: 0.6946Epoch 00006: val_loss improved from 0.00366 to 0.00357, saving model to saved_model/best_model_with_adagrad.hdf5
1712/1712 [==============================] - 13s - loss: 0.0049 - acc: 0.6945 - val_loss: 0.0036 - val_acc: 0.6963
Epoch 8/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0047 - acc: 0.6969Epoch 00007: val_loss improved from 0.00357 to 0.00337, saving model to saved_model/best_model_with_adagrad.hdf5
1712/1712 [==============================] - 14s - loss: 0.0047 - acc: 0.6963 - val_loss: 0.0034 - val_acc: 0.6963
Epoch 9/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0046 - acc: 0.7093Epoch 00008: val_loss improved from 0.00337 to 0.00301, saving model to saved_model/best_model_with_adagrad.hdf5
1712/1712 [==============================] - 14s - loss: 0.0046 - acc: 0.7079 - val_loss: 0.0030 - val_acc: 0.6963
Epoch 10/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0042 - acc: 0.6981Epoch 00009: val_loss improved from 0.00301 to 0.00279, saving model to saved_model/best_model_with_adagrad.hdf5
1712/1712 [==============================] - 13s - loss: 0.0042 - acc: 0.6986 - val_loss: 0.0028 - val_acc: 0.6963
Epoch 11/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0041 - acc: 0.7081Epoch 00010: val_loss did not improve
1712/1712 [==============================] - 13s - loss: 0.0041 - acc: 0.7097 - val_loss: 0.0029 - val_acc: 0.6986
Epoch 12/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0039 - acc: 0.7022Epoch 00011: val_loss improved from 0.00279 to 0.00253, saving model to saved_model/best_model_with_adagrad.hdf5
1712/1712 [==============================] - 13s - loss: 0.0039 - acc: 0.7009 - val_loss: 0.0025 - val_acc: 0.6986
Epoch 13/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0037 - acc: 0.7087Epoch 00012: val_loss improved from 0.00253 to 0.00230, saving model to saved_model/best_model_with_adagrad.hdf5
1712/1712 [==============================] - 13s - loss: 0.0037 - acc: 0.7085 - val_loss: 0.0023 - val_acc: 0.7033
Epoch 14/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0036 - acc: 0.7064Epoch 00013: val_loss improved from 0.00230 to 0.00224, saving model to saved_model/best_model_with_adagrad.hdf5
1712/1712 [==============================] - 13s - loss: 0.0036 - acc: 0.7062 - val_loss: 0.0022 - val_acc: 0.6986
Epoch 15/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0035 - acc: 0.7046Epoch 00014: val_loss improved from 0.00224 to 0.00214, saving model to saved_model/best_model_with_adagrad.hdf5
1712/1712 [==============================] - 13s - loss: 0.0035 - acc: 0.7056 - val_loss: 0.0021 - val_acc: 0.7033
Epoch 16/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0034 - acc: 0.7064Epoch 00015: val_loss did not improve
1712/1712 [==============================] - 13s - loss: 0.0034 - acc: 0.7074 - val_loss: 0.0025 - val_acc: 0.7033
Epoch 17/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0033 - acc: 0.7064Epoch 00016: val_loss improved from 0.00214 to 0.00213, saving model to saved_model/best_model_with_adagrad.hdf5
1712/1712 [==============================] - 14s - loss: 0.0033 - acc: 0.7079 - val_loss: 0.0021 - val_acc: 0.7009
Epoch 18/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0032 - acc: 0.7075Epoch 00017: val_loss improved from 0.00213 to 0.00195, saving model to saved_model/best_model_with_adagrad.hdf5
1712/1712 [==============================] - 13s - loss: 0.0032 - acc: 0.7074 - val_loss: 0.0020 - val_acc: 0.7103
Epoch 19/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0031 - acc: 0.7046Epoch 00018: val_loss improved from 0.00195 to 0.00191, saving model to saved_model/best_model_with_adagrad.hdf5
1712/1712 [==============================] - 13s - loss: 0.0031 - acc: 0.7033 - val_loss: 0.0019 - val_acc: 0.7126
Epoch 20/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0030 - acc: 0.7005Epoch 00019: val_loss improved from 0.00191 to 0.00183, saving model to saved_model/best_model_with_adagrad.hdf5
1712/1712 [==============================] - 13s - loss: 0.0030 - acc: 0.6992 - val_loss: 0.0018 - val_acc: 0.7079
Epoch 21/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0030 - acc: 0.7188Epoch 00020: val_loss did not improve
1712/1712 [==============================] - 13s - loss: 0.0030 - acc: 0.7185 - val_loss: 0.0020 - val_acc: 0.7079
Epoch 22/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0029 - acc: 0.7158Epoch 00021: val_loss improved from 0.00183 to 0.00172, saving model to saved_model/best_model_with_adagrad.hdf5
1712/1712 [==============================] - 13s - loss: 0.0029 - acc: 0.7138 - val_loss: 0.0017 - val_acc: 0.7196
Epoch 23/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0029 - acc: 0.7158Epoch 00022: val_loss did not improve
1712/1712 [==============================] - 13s - loss: 0.0029 - acc: 0.7150 - val_loss: 0.0018 - val_acc: 0.7103
Epoch 24/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0028 - acc: 0.7117Epoch 00023: val_loss did not improve
1712/1712 [==============================] - 13s - loss: 0.0028 - acc: 0.7120 - val_loss: 0.0018 - val_acc: 0.7150
Epoch 25/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0028 - acc: 0.7146Epoch 00024: val_loss improved from 0.00172 to 0.00170, saving model to saved_model/best_model_with_adagrad.hdf5
1712/1712 [==============================] - 13s - loss: 0.0028 - acc: 0.7155 - val_loss: 0.0017 - val_acc: 0.7126
Epoch 26/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0027 - acc: 0.7205Epoch 00025: val_loss did not improve
1712/1712 [==============================] - 14s - loss: 0.0027 - acc: 0.7190 - val_loss: 0.0018 - val_acc: 0.7079
Epoch 27/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0027 - acc: 0.7241Epoch 00026: val_loss did not improve
1712/1712 [==============================] - 16s - loss: 0.0027 - acc: 0.7255 - val_loss: 0.0018 - val_acc: 0.7033
Epoch 28/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0026 - acc: 0.7246Epoch 00027: val_loss improved from 0.00170 to 0.00164, saving model to saved_model/best_model_with_adagrad.hdf5
1712/1712 [==============================] - 15s - loss: 0.0026 - acc: 0.7243 - val_loss: 0.0016 - val_acc: 0.7290
Epoch 29/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0026 - acc: 0.7217Epoch 00028: val_loss improved from 0.00164 to 0.00161, saving model to saved_model/best_model_with_adagrad.hdf5
1712/1712 [==============================] - 15s - loss: 0.0026 - acc: 0.7214 - val_loss: 0.0016 - val_acc: 0.7266
Epoch 30/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0026 - acc: 0.7241Epoch 00029: val_loss did not improve
1712/1712 [==============================] - 13s - loss: 0.0026 - acc: 0.7249 - val_loss: 0.0017 - val_acc: 0.7243
Train on 1712 samples, validate on 428 samples
Epoch 1/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0273 - acc: 0.4487Epoch 00000: val_loss improved from inf to 0.00705, saving model to saved_model/best_model_with_adadelta.hdf5
1712/1712 [==============================] - 16s - loss: 0.0272 - acc: 0.4498 - val_loss: 0.0071 - val_acc: 0.6963
Epoch 2/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0150 - acc: 0.5660Epoch 00001: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0150 - acc: 0.5666 - val_loss: 0.0077 - val_acc: 0.6963
Epoch 3/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0114 - acc: 0.6061Epoch 00002: val_loss improved from 0.00705 to 0.00489, saving model to saved_model/best_model_with_adadelta.hdf5
1712/1712 [==============================] - 15s - loss: 0.0114 - acc: 0.6057 - val_loss: 0.0049 - val_acc: 0.6963
Epoch 4/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0096 - acc: 0.6232Epoch 00003: val_loss improved from 0.00489 to 0.00455, saving model to saved_model/best_model_with_adadelta.hdf5
1712/1712 [==============================] - 17s - loss: 0.0096 - acc: 0.6232 - val_loss: 0.0045 - val_acc: 0.6963
Epoch 5/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0086 - acc: 0.6279Epoch 00004: val_loss did not improve
1712/1712 [==============================] - 17s - loss: 0.0086 - acc: 0.6273 - val_loss: 0.0047 - val_acc: 0.6963
Epoch 6/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0079 - acc: 0.6362Epoch 00005: val_loss improved from 0.00455 to 0.00428, saving model to saved_model/best_model_with_adadelta.hdf5
1712/1712 [==============================] - 16s - loss: 0.0078 - acc: 0.6379 - val_loss: 0.0043 - val_acc: 0.6963
Epoch 7/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0075 - acc: 0.6557Epoch 00006: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0074 - acc: 0.6560 - val_loss: 0.0054 - val_acc: 0.6963
Epoch 8/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0071 - acc: 0.6840Epoch 00007: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0071 - acc: 0.6840 - val_loss: 0.0043 - val_acc: 0.6963
Epoch 9/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0066 - acc: 0.6639Epoch 00008: val_loss improved from 0.00428 to 0.00415, saving model to saved_model/best_model_with_adadelta.hdf5
1712/1712 [==============================] - 15s - loss: 0.0067 - acc: 0.6647 - val_loss: 0.0041 - val_acc: 0.6963
Epoch 10/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0065 - acc: 0.6834Epoch 00009: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0065 - acc: 0.6834 - val_loss: 0.0042 - val_acc: 0.6963
Epoch 11/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0063 - acc: 0.6816Epoch 00010: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0062 - acc: 0.6828 - val_loss: 0.0042 - val_acc: 0.6963
Epoch 12/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0060 - acc: 0.6840Epoch 00011: val_loss improved from 0.00415 to 0.00402, saving model to saved_model/best_model_with_adadelta.hdf5
1712/1712 [==============================] - 15s - loss: 0.0060 - acc: 0.6834 - val_loss: 0.0040 - val_acc: 0.6963
Epoch 13/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0059 - acc: 0.6846Epoch 00012: val_loss did not improve
1712/1712 [==============================] - 14s - loss: 0.0059 - acc: 0.6869 - val_loss: 0.0041 - val_acc: 0.6963
Epoch 14/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0057 - acc: 0.6969Epoch 00013: val_loss did not improve
1712/1712 [==============================] - 14s - loss: 0.0057 - acc: 0.6974 - val_loss: 0.0040 - val_acc: 0.6963
Epoch 15/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0056 - acc: 0.6910Epoch 00014: val_loss improved from 0.00402 to 0.00401, saving model to saved_model/best_model_with_adadelta.hdf5
1712/1712 [==============================] - 15s - loss: 0.0056 - acc: 0.6910 - val_loss: 0.0040 - val_acc: 0.6963
Epoch 16/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0055 - acc: 0.7022Epoch 00015: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0055 - acc: 0.6998 - val_loss: 0.0041 - val_acc: 0.6963
Epoch 17/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0054 - acc: 0.6916Epoch 00016: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0054 - acc: 0.6928 - val_loss: 0.0042 - val_acc: 0.6963
Epoch 18/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0053 - acc: 0.6975Epoch 00017: val_loss improved from 0.00401 to 0.00399, saving model to saved_model/best_model_with_adadelta.hdf5
1712/1712 [==============================] - 16s - loss: 0.0053 - acc: 0.6980 - val_loss: 0.0040 - val_acc: 0.6963
Epoch 19/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0052 - acc: 0.7040Epoch 00018: val_loss did not improve
1712/1712 [==============================] - 14s - loss: 0.0053 - acc: 0.7015 - val_loss: 0.0041 - val_acc: 0.6963
Epoch 20/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0050 - acc: 0.7058Epoch 00019: val_loss did not improve
1712/1712 [==============================] - 14s - loss: 0.0050 - acc: 0.7039 - val_loss: 0.0042 - val_acc: 0.6963
Epoch 21/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0050 - acc: 0.7034Epoch 00020: val_loss improved from 0.00399 to 0.00382, saving model to saved_model/best_model_with_adadelta.hdf5
1712/1712 [==============================] - 15s - loss: 0.0050 - acc: 0.7044 - val_loss: 0.0038 - val_acc: 0.6963
Epoch 22/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0049 - acc: 0.6993Epoch 00021: val_loss improved from 0.00382 to 0.00368, saving model to saved_model/best_model_with_adadelta.hdf5
1712/1712 [==============================] - 15s - loss: 0.0049 - acc: 0.6992 - val_loss: 0.0037 - val_acc: 0.6963
Epoch 23/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0048 - acc: 0.7034Epoch 00022: val_loss improved from 0.00368 to 0.00364, saving model to saved_model/best_model_with_adadelta.hdf5
1712/1712 [==============================] - 16s - loss: 0.0048 - acc: 0.7033 - val_loss: 0.0036 - val_acc: 0.6963
Epoch 24/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0048 - acc: 0.6969Epoch 00023: val_loss improved from 0.00364 to 0.00358, saving model to saved_model/best_model_with_adadelta.hdf5
1712/1712 [==============================] - 15s - loss: 0.0048 - acc: 0.6986 - val_loss: 0.0036 - val_acc: 0.6963
Epoch 25/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0046 - acc: 0.6999Epoch 00024: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0046 - acc: 0.6998 - val_loss: 0.0038 - val_acc: 0.6963
Epoch 26/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0046 - acc: 0.7034Epoch 00025: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0046 - acc: 0.7033 - val_loss: 0.0036 - val_acc: 0.6963
Epoch 27/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0045 - acc: 0.7075Epoch 00026: val_loss improved from 0.00358 to 0.00344, saving model to saved_model/best_model_with_adadelta.hdf5
1712/1712 [==============================] - 15s - loss: 0.0045 - acc: 0.7056 - val_loss: 0.0034 - val_acc: 0.6963
Epoch 28/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0044 - acc: 0.7028Epoch 00027: val_loss improved from 0.00344 to 0.00342, saving model to saved_model/best_model_with_adadelta.hdf5
1712/1712 [==============================] - 15s - loss: 0.0044 - acc: 0.7021 - val_loss: 0.0034 - val_acc: 0.6963
Epoch 29/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0043 - acc: 0.7040Epoch 00028: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0043 - acc: 0.7033 - val_loss: 0.0034 - val_acc: 0.6963
Epoch 30/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0043 - acc: 0.7075Epoch 00029: val_loss did not improve
1712/1712 [==============================] - 16s - loss: 0.0043 - acc: 0.7079 - val_loss: 0.0037 - val_acc: 0.6963
Train on 1712 samples, validate on 428 samples
Epoch 1/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0201 - acc: 0.5389Epoch 00000: val_loss improved from inf to 0.00525, saving model to saved_model/best_model_with_adam.hdf5
1712/1712 [==============================] - 14s - loss: 0.0201 - acc: 0.5380 - val_loss: 0.0052 - val_acc: 0.6963
Epoch 2/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0078 - acc: 0.6415Epoch 00001: val_loss improved from 0.00525 to 0.00415, saving model to saved_model/best_model_with_adam.hdf5
1712/1712 [==============================] - 14s - loss: 0.0078 - acc: 0.6402 - val_loss: 0.0041 - val_acc: 0.6963
Epoch 3/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0060 - acc: 0.6616Epoch 00002: val_loss improved from 0.00415 to 0.00358, saving model to saved_model/best_model_with_adam.hdf5
1712/1712 [==============================] - 15s - loss: 0.0060 - acc: 0.6624 - val_loss: 0.0036 - val_acc: 0.6963
Epoch 4/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0053 - acc: 0.6869Epoch 00003: val_loss improved from 0.00358 to 0.00286, saving model to saved_model/best_model_with_adam.hdf5
1712/1712 [==============================] - 15s - loss: 0.0053 - acc: 0.6852 - val_loss: 0.0029 - val_acc: 0.6963
Epoch 5/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0046 - acc: 0.6958Epoch 00004: val_loss did not improve
1712/1712 [==============================] - 16s - loss: 0.0046 - acc: 0.6939 - val_loss: 0.0032 - val_acc: 0.6963
Epoch 6/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0043 - acc: 0.6981Epoch 00005: val_loss improved from 0.00286 to 0.00239, saving model to saved_model/best_model_with_adam.hdf5
1712/1712 [==============================] - 14s - loss: 0.0043 - acc: 0.6992 - val_loss: 0.0024 - val_acc: 0.6986
Epoch 7/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0037 - acc: 0.6952Epoch 00006: val_loss improved from 0.00239 to 0.00218, saving model to saved_model/best_model_with_adam.hdf5
1712/1712 [==============================] - 15s - loss: 0.0037 - acc: 0.6957 - val_loss: 0.0022 - val_acc: 0.6986
Epoch 8/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0035 - acc: 0.7028Epoch 00007: val_loss improved from 0.00218 to 0.00216, saving model to saved_model/best_model_with_adam.hdf5
1712/1712 [==============================] - 15s - loss: 0.0035 - acc: 0.7027 - val_loss: 0.0022 - val_acc: 0.7056
Epoch 9/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0033 - acc: 0.7070Epoch 00008: val_loss improved from 0.00216 to 0.00201, saving model to saved_model/best_model_with_adam.hdf5
1712/1712 [==============================] - 14s - loss: 0.0033 - acc: 0.7062 - val_loss: 0.0020 - val_acc: 0.7033
Epoch 10/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0032 - acc: 0.7064Epoch 00009: val_loss improved from 0.00201 to 0.00190, saving model to saved_model/best_model_with_adam.hdf5
1712/1712 [==============================] - 14s - loss: 0.0032 - acc: 0.7074 - val_loss: 0.0019 - val_acc: 0.7056
Epoch 11/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0029 - acc: 0.7217Epoch 00010: val_loss improved from 0.00190 to 0.00188, saving model to saved_model/best_model_with_adam.hdf5
1712/1712 [==============================] - 14s - loss: 0.0029 - acc: 0.7220 - val_loss: 0.0019 - val_acc: 0.7103
Epoch 12/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0029 - acc: 0.7223Epoch 00011: val_loss improved from 0.00188 to 0.00186, saving model to saved_model/best_model_with_adam.hdf5
1712/1712 [==============================] - 14s - loss: 0.0029 - acc: 0.7231 - val_loss: 0.0019 - val_acc: 0.7033
Epoch 13/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0028 - acc: 0.7093Epoch 00012: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0028 - acc: 0.7097 - val_loss: 0.0019 - val_acc: 0.7383
Epoch 14/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0027 - acc: 0.7205Epoch 00013: val_loss improved from 0.00186 to 0.00185, saving model to saved_model/best_model_with_adam.hdf5
1712/1712 [==============================] - 14s - loss: 0.0027 - acc: 0.7196 - val_loss: 0.0018 - val_acc: 0.7056
Epoch 15/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0027 - acc: 0.7199Epoch 00014: val_loss improved from 0.00185 to 0.00169, saving model to saved_model/best_model_with_adam.hdf5
1712/1712 [==============================] - 14s - loss: 0.0027 - acc: 0.7208 - val_loss: 0.0017 - val_acc: 0.7126
Epoch 16/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0024 - acc: 0.7276Epoch 00015: val_loss did not improve
1712/1712 [==============================] - 14s - loss: 0.0025 - acc: 0.7261 - val_loss: 0.0018 - val_acc: 0.7150
Epoch 17/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0025 - acc: 0.7282Epoch 00016: val_loss did not improve
1712/1712 [==============================] - 14s - loss: 0.0025 - acc: 0.7296 - val_loss: 0.0018 - val_acc: 0.7266
Epoch 18/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0024 - acc: 0.7235Epoch 00017: val_loss improved from 0.00169 to 0.00161, saving model to saved_model/best_model_with_adam.hdf5
1712/1712 [==============================] - 14s - loss: 0.0024 - acc: 0.7237 - val_loss: 0.0016 - val_acc: 0.7336
Epoch 19/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0023 - acc: 0.7300Epoch 00018: val_loss improved from 0.00161 to 0.00153, saving model to saved_model/best_model_with_adam.hdf5
1712/1712 [==============================] - 14s - loss: 0.0023 - acc: 0.7307 - val_loss: 0.0015 - val_acc: 0.7243
Epoch 20/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0022 - acc: 0.7335Epoch 00019: val_loss improved from 0.00153 to 0.00146, saving model to saved_model/best_model_with_adam.hdf5
1712/1712 [==============================] - 14s - loss: 0.0022 - acc: 0.7331 - val_loss: 0.0015 - val_acc: 0.7430
Epoch 21/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0022 - acc: 0.7400Epoch 00020: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0022 - acc: 0.7412 - val_loss: 0.0015 - val_acc: 0.7266
Epoch 22/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0021 - acc: 0.7388Epoch 00021: val_loss improved from 0.00146 to 0.00143, saving model to saved_model/best_model_with_adam.hdf5
1712/1712 [==============================] - 15s - loss: 0.0021 - acc: 0.7383 - val_loss: 0.0014 - val_acc: 0.7383
Epoch 23/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0021 - acc: 0.7282Epoch 00022: val_loss did not improve
1712/1712 [==============================] - 16s - loss: 0.0021 - acc: 0.7284 - val_loss: 0.0015 - val_acc: 0.7430
Epoch 24/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0021 - acc: 0.7429Epoch 00023: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0021 - acc: 0.7436 - val_loss: 0.0014 - val_acc: 0.7593
Epoch 25/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0020 - acc: 0.7500Epoch 00024: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0020 - acc: 0.7471 - val_loss: 0.0015 - val_acc: 0.7570
Epoch 26/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0019 - acc: 0.7488Epoch 00025: val_loss improved from 0.00143 to 0.00137, saving model to saved_model/best_model_with_adam.hdf5
1712/1712 [==============================] - 15s - loss: 0.0019 - acc: 0.7477 - val_loss: 0.0014 - val_acc: 0.7640
Epoch 27/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0019 - acc: 0.7559Epoch 00026: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0019 - acc: 0.7535 - val_loss: 0.0014 - val_acc: 0.7453
Epoch 28/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0019 - acc: 0.7506Epoch 00027: val_loss did not improve
1712/1712 [==============================] - 14s - loss: 0.0019 - acc: 0.7518 - val_loss: 0.0014 - val_acc: 0.7477
Epoch 29/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0019 - acc: 0.7577Epoch 00028: val_loss did not improve
1712/1712 [==============================] - 16s - loss: 0.0019 - acc: 0.7582 - val_loss: 0.0015 - val_acc: 0.7617
Epoch 30/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0019 - acc: 0.7529Epoch 00029: val_loss improved from 0.00137 to 0.00132, saving model to saved_model/best_model_with_adam.hdf5
1712/1712 [==============================] - 16s - loss: 0.0019 - acc: 0.7535 - val_loss: 0.0013 - val_acc: 0.7523
Train on 1712 samples, validate on 428 samples
Epoch 1/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0280 - acc: 0.4676Epoch 00000: val_loss improved from inf to 0.00492, saving model to saved_model/best_model_with_adamax.hdf5
1712/1712 [==============================] - 15s - loss: 0.0279 - acc: 0.4690 - val_loss: 0.0049 - val_acc: 0.6963
Epoch 2/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0094 - acc: 0.5967Epoch 00001: val_loss improved from 0.00492 to 0.00462, saving model to saved_model/best_model_with_adamax.hdf5
1712/1712 [==============================] - 14s - loss: 0.0094 - acc: 0.5981 - val_loss: 0.0046 - val_acc: 0.6963
Epoch 3/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0080 - acc: 0.6421Epoch 00002: val_loss improved from 0.00462 to 0.00402, saving model to saved_model/best_model_with_adamax.hdf5
1712/1712 [==============================] - 15s - loss: 0.0080 - acc: 0.6414 - val_loss: 0.0040 - val_acc: 0.6963
Epoch 4/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0072 - acc: 0.6604Epoch 00003: val_loss improved from 0.00402 to 0.00390, saving model to saved_model/best_model_with_adamax.hdf5
1712/1712 [==============================] - 15s - loss: 0.0072 - acc: 0.6600 - val_loss: 0.0039 - val_acc: 0.6963
Epoch 5/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0066 - acc: 0.6621Epoch 00004: val_loss improved from 0.00390 to 0.00360, saving model to saved_model/best_model_with_adamax.hdf5
1712/1712 [==============================] - 16s - loss: 0.0066 - acc: 0.6624 - val_loss: 0.0036 - val_acc: 0.6963
Epoch 6/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0060 - acc: 0.6763Epoch 00005: val_loss improved from 0.00360 to 0.00349, saving model to saved_model/best_model_with_adamax.hdf5
1712/1712 [==============================] - 15s - loss: 0.0060 - acc: 0.6746 - val_loss: 0.0035 - val_acc: 0.6963
Epoch 7/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0056 - acc: 0.6728Epoch 00006: val_loss improved from 0.00349 to 0.00340, saving model to saved_model/best_model_with_adamax.hdf5
1712/1712 [==============================] - 15s - loss: 0.0056 - acc: 0.6735 - val_loss: 0.0034 - val_acc: 0.6963
Epoch 8/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0054 - acc: 0.6816Epoch 00007: val_loss improved from 0.00340 to 0.00314, saving model to saved_model/best_model_with_adamax.hdf5
1712/1712 [==============================] - 15s - loss: 0.0054 - acc: 0.6811 - val_loss: 0.0031 - val_acc: 0.6986
Epoch 9/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0049 - acc: 0.6792Epoch 00008: val_loss improved from 0.00314 to 0.00287, saving model to saved_model/best_model_with_adamax.hdf5
1712/1712 [==============================] - 15s - loss: 0.0049 - acc: 0.6805 - val_loss: 0.0029 - val_acc: 0.6963
Epoch 10/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0047 - acc: 0.6975Epoch 00009: val_loss improved from 0.00287 to 0.00258, saving model to saved_model/best_model_with_adamax.hdf5
1712/1712 [==============================] - 14s - loss: 0.0047 - acc: 0.6974 - val_loss: 0.0026 - val_acc: 0.6963
Epoch 11/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0044 - acc: 0.6922Epoch 00010: val_loss did not improve
1712/1712 [==============================] - 14s - loss: 0.0044 - acc: 0.6939 - val_loss: 0.0028 - val_acc: 0.6963
Epoch 12/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0043 - acc: 0.7022Epoch 00011: val_loss improved from 0.00258 to 0.00232, saving model to saved_model/best_model_with_adamax.hdf5
1712/1712 [==============================] - 16s - loss: 0.0043 - acc: 0.7015 - val_loss: 0.0023 - val_acc: 0.6963
Epoch 13/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0040 - acc: 0.7046Epoch 00012: val_loss improved from 0.00232 to 0.00213, saving model to saved_model/best_model_with_adamax.hdf5
1712/1712 [==============================] - 15s - loss: 0.0040 - acc: 0.7056 - val_loss: 0.0021 - val_acc: 0.6963
Epoch 14/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0039 - acc: 0.7011Epoch 00013: val_loss did not improve
1712/1712 [==============================] - 14s - loss: 0.0039 - acc: 0.7015 - val_loss: 0.0022 - val_acc: 0.7009
Epoch 15/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0038 - acc: 0.7075Epoch 00014: val_loss improved from 0.00213 to 0.00211, saving model to saved_model/best_model_with_adamax.hdf5
1712/1712 [==============================] - 15s - loss: 0.0038 - acc: 0.7085 - val_loss: 0.0021 - val_acc: 0.7009
Epoch 16/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0036 - acc: 0.7087Epoch 00015: val_loss improved from 0.00211 to 0.00184, saving model to saved_model/best_model_with_adamax.hdf5
1712/1712 [==============================] - 18s - loss: 0.0036 - acc: 0.7079 - val_loss: 0.0018 - val_acc: 0.7079
Epoch 17/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0035 - acc: 0.7034Epoch 00016: val_loss did not improve
1712/1712 [==============================] - 16s - loss: 0.0035 - acc: 0.7027 - val_loss: 0.0019 - val_acc: 0.7079
Epoch 18/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0034 - acc: 0.7064Epoch 00017: val_loss improved from 0.00184 to 0.00183, saving model to saved_model/best_model_with_adamax.hdf5
1712/1712 [==============================] - 15s - loss: 0.0034 - acc: 0.7074 - val_loss: 0.0018 - val_acc: 0.7126
Epoch 19/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0032 - acc: 0.7258Epoch 00018: val_loss improved from 0.00183 to 0.00174, saving model to saved_model/best_model_with_adamax.hdf5
1712/1712 [==============================] - 16s - loss: 0.0032 - acc: 0.7255 - val_loss: 0.0017 - val_acc: 0.7079
Epoch 20/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0032 - acc: 0.7158Epoch 00019: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0032 - acc: 0.7144 - val_loss: 0.0018 - val_acc: 0.7079
Epoch 21/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0031 - acc: 0.7229Epoch 00020: val_loss improved from 0.00174 to 0.00167, saving model to saved_model/best_model_with_adamax.hdf5
1712/1712 [==============================] - 15s - loss: 0.0031 - acc: 0.7220 - val_loss: 0.0017 - val_acc: 0.7150
Epoch 22/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0030 - acc: 0.7264Epoch 00021: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0030 - acc: 0.7261 - val_loss: 0.0017 - val_acc: 0.7150
Epoch 23/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0029 - acc: 0.7193Epoch 00022: val_loss did not improve
1712/1712 [==============================] - 16s - loss: 0.0029 - acc: 0.7196 - val_loss: 0.0020 - val_acc: 0.7103
Epoch 24/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0029 - acc: 0.7217Epoch 00023: val_loss improved from 0.00167 to 0.00161, saving model to saved_model/best_model_with_adamax.hdf5
1712/1712 [==============================] - 15s - loss: 0.0029 - acc: 0.7243 - val_loss: 0.0016 - val_acc: 0.7126
Epoch 25/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0028 - acc: 0.7246Epoch 00024: val_loss did not improve
1712/1712 [==============================] - 14s - loss: 0.0028 - acc: 0.7249 - val_loss: 0.0016 - val_acc: 0.7150
Epoch 26/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0028 - acc: 0.7146Epoch 00025: val_loss did not improve
1712/1712 [==============================] - 14s - loss: 0.0028 - acc: 0.7144 - val_loss: 0.0017 - val_acc: 0.7220
Epoch 27/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0028 - acc: 0.7229Epoch 00026: val_loss improved from 0.00161 to 0.00154, saving model to saved_model/best_model_with_adamax.hdf5
1712/1712 [==============================] - 15s - loss: 0.0028 - acc: 0.7231 - val_loss: 0.0015 - val_acc: 0.7196
Epoch 28/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0027 - acc: 0.7329Epoch 00027: val_loss did not improve
1712/1712 [==============================] - 17s - loss: 0.0027 - acc: 0.7325 - val_loss: 0.0016 - val_acc: 0.7126
Epoch 29/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0026 - acc: 0.7323Epoch 00028: val_loss did not improve
1712/1712 [==============================] - 14s - loss: 0.0026 - acc: 0.7319 - val_loss: 0.0017 - val_acc: 0.7196
Epoch 30/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0026 - acc: 0.7323Epoch 00029: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0026 - acc: 0.7313 - val_loss: 0.0017 - val_acc: 0.7196
Train on 1712 samples, validate on 428 samples
Epoch 1/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0208 - acc: 0.5890Epoch 00000: val_loss improved from inf to 0.00625, saving model to saved_model/best_model_with_nadam.hdf5
1712/1712 [==============================] - 18s - loss: 0.0207 - acc: 0.5876 - val_loss: 0.0062 - val_acc: 0.6963
Epoch 2/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0121 - acc: 0.6739Epoch 00001: val_loss improved from 0.00625 to 0.00527, saving model to saved_model/best_model_with_nadam.hdf5
1712/1712 [==============================] - 15s - loss: 0.0120 - acc: 0.6735 - val_loss: 0.0053 - val_acc: 0.6963
Epoch 3/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0067 - acc: 0.6851Epoch 00002: val_loss improved from 0.00527 to 0.00418, saving model to saved_model/best_model_with_nadam.hdf5
1712/1712 [==============================] - 15s - loss: 0.0066 - acc: 0.6863 - val_loss: 0.0042 - val_acc: 0.6963
Epoch 4/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0057 - acc: 0.6958Epoch 00003: val_loss improved from 0.00418 to 0.00379, saving model to saved_model/best_model_with_nadam.hdf5
1712/1712 [==============================] - 18s - loss: 0.0057 - acc: 0.6945 - val_loss: 0.0038 - val_acc: 0.6963
Epoch 5/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0051 - acc: 0.7087Epoch 00004: val_loss did not improve
1712/1712 [==============================] - 17s - loss: 0.0051 - acc: 0.7079 - val_loss: 0.0042 - val_acc: 0.6963
Epoch 6/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0047 - acc: 0.7040Epoch 00005: val_loss improved from 0.00379 to 0.00333, saving model to saved_model/best_model_with_nadam.hdf5
1712/1712 [==============================] - 17s - loss: 0.0047 - acc: 0.7050 - val_loss: 0.0033 - val_acc: 0.6939
Epoch 7/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0042 - acc: 0.6958Epoch 00006: val_loss improved from 0.00333 to 0.00294, saving model to saved_model/best_model_with_nadam.hdf5
1712/1712 [==============================] - 20s - loss: 0.0042 - acc: 0.6968 - val_loss: 0.0029 - val_acc: 0.6939
Epoch 8/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0038 - acc: 0.7034Epoch 00007: val_loss improved from 0.00294 to 0.00257, saving model to saved_model/best_model_with_nadam.hdf5
1712/1712 [==============================] - 23s - loss: 0.0038 - acc: 0.7033 - val_loss: 0.0026 - val_acc: 0.6963
Epoch 9/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0036 - acc: 0.7022Epoch 00008: val_loss improved from 0.00257 to 0.00252, saving model to saved_model/best_model_with_nadam.hdf5
1712/1712 [==============================] - 21s - loss: 0.0036 - acc: 0.7033 - val_loss: 0.0025 - val_acc: 0.6963
Epoch 10/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0032 - acc: 0.7052Epoch 00009: val_loss improved from 0.00252 to 0.00226, saving model to saved_model/best_model_with_nadam.hdf5
1712/1712 [==============================] - 20s - loss: 0.0032 - acc: 0.7044 - val_loss: 0.0023 - val_acc: 0.6986
Epoch 11/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0031 - acc: 0.7111Epoch 00010: val_loss improved from 0.00226 to 0.00217, saving model to saved_model/best_model_with_nadam.hdf5
1712/1712 [==============================] - 23s - loss: 0.0031 - acc: 0.7109 - val_loss: 0.0022 - val_acc: 0.7009
Epoch 12/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0029 - acc: 0.7058Epoch 00011: val_loss did not improve
1712/1712 [==============================] - 21s - loss: 0.0029 - acc: 0.7068 - val_loss: 0.0024 - val_acc: 0.6986
Epoch 13/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0028 - acc: 0.7170Epoch 00012: val_loss improved from 0.00217 to 0.00195, saving model to saved_model/best_model_with_nadam.hdf5
1712/1712 [==============================] - 21s - loss: 0.0028 - acc: 0.7179 - val_loss: 0.0020 - val_acc: 0.7009
Epoch 14/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0026 - acc: 0.7170Epoch 00013: val_loss improved from 0.00195 to 0.00174, saving model to saved_model/best_model_with_nadam.hdf5
1712/1712 [==============================] - 21s - loss: 0.0026 - acc: 0.7173 - val_loss: 0.0017 - val_acc: 0.7103
Epoch 15/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0025 - acc: 0.7176Epoch 00014: val_loss improved from 0.00174 to 0.00172, saving model to saved_model/best_model_with_nadam.hdf5
1712/1712 [==============================] - 20s - loss: 0.0025 - acc: 0.7167 - val_loss: 0.0017 - val_acc: 0.7103
Epoch 16/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0024 - acc: 0.7158Epoch 00015: val_loss did not improve
1712/1712 [==============================] - 22s - loss: 0.0024 - acc: 0.7138 - val_loss: 0.0022 - val_acc: 0.7056
Epoch 17/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0024 - acc: 0.7193Epoch 00016: val_loss improved from 0.00172 to 0.00168, saving model to saved_model/best_model_with_nadam.hdf5
1712/1712 [==============================] - 20s - loss: 0.0024 - acc: 0.7202 - val_loss: 0.0017 - val_acc: 0.7103
Epoch 18/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0023 - acc: 0.7329Epoch 00017: val_loss improved from 0.00168 to 0.00166, saving model to saved_model/best_model_with_nadam.hdf5
1712/1712 [==============================] - 20s - loss: 0.0023 - acc: 0.7336 - val_loss: 0.0017 - val_acc: 0.7243
Epoch 19/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0022 - acc: 0.7412Epoch 00018: val_loss did not improve
1712/1712 [==============================] - 20s - loss: 0.0022 - acc: 0.7412 - val_loss: 0.0017 - val_acc: 0.7243
Epoch 20/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0021 - acc: 0.7382Epoch 00019: val_loss improved from 0.00166 to 0.00157, saving model to saved_model/best_model_with_nadam.hdf5
1712/1712 [==============================] - 20s - loss: 0.0021 - acc: 0.7389 - val_loss: 0.0016 - val_acc: 0.7150
Epoch 21/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0021 - acc: 0.7364Epoch 00020: val_loss improved from 0.00157 to 0.00150, saving model to saved_model/best_model_with_nadam.hdf5
1712/1712 [==============================] - 20s - loss: 0.0021 - acc: 0.7366 - val_loss: 0.0015 - val_acc: 0.7150
Epoch 22/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0020 - acc: 0.7370Epoch 00021: val_loss did not improve
1712/1712 [==============================] - 20s - loss: 0.0020 - acc: 0.7377 - val_loss: 0.0015 - val_acc: 0.7266
Epoch 23/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0019 - acc: 0.7429Epoch 00022: val_loss did not improve
1712/1712 [==============================] - 20s - loss: 0.0019 - acc: 0.7447 - val_loss: 0.0016 - val_acc: 0.7220
Epoch 24/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0019 - acc: 0.7465Epoch 00023: val_loss did not improve
1712/1712 [==============================] - 21s - loss: 0.0019 - acc: 0.7465 - val_loss: 0.0017 - val_acc: 0.7453
Epoch 25/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0018 - acc: 0.7453Epoch 00024: val_loss did not improve
1712/1712 [==============================] - 20s - loss: 0.0018 - acc: 0.7442 - val_loss: 0.0015 - val_acc: 0.7360
Epoch 26/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0018 - acc: 0.7488Epoch 00025: val_loss improved from 0.00150 to 0.00148, saving model to saved_model/best_model_with_nadam.hdf5
1712/1712 [==============================] - 20s - loss: 0.0018 - acc: 0.7488 - val_loss: 0.0015 - val_acc: 0.7453
Epoch 27/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0017 - acc: 0.7524Epoch 00026: val_loss improved from 0.00148 to 0.00141, saving model to saved_model/best_model_with_nadam.hdf5
1712/1712 [==============================] - 20s - loss: 0.0018 - acc: 0.7535 - val_loss: 0.0014 - val_acc: 0.7430
Epoch 28/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0017 - acc: 0.7529Epoch 00027: val_loss improved from 0.00141 to 0.00139, saving model to saved_model/best_model_with_nadam.hdf5
1712/1712 [==============================] - 20s - loss: 0.0017 - acc: 0.7529 - val_loss: 0.0014 - val_acc: 0.7430
Epoch 29/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0017 - acc: 0.7518Epoch 00028: val_loss did not improve
1712/1712 [==============================] - 20s - loss: 0.0017 - acc: 0.7518 - val_loss: 0.0014 - val_acc: 0.7383
Epoch 30/30
1696/1712 [============================>.] - ETA: 0s - loss: 0.0016 - acc: 0.7553Epoch 00029: val_loss did not improve
1712/1712 [==============================] - 21s - loss: 0.0016 - acc: 0.7553 - val_loss: 0.0014 - val_acc: 0.7477
In [28]:
model.compile(loss='mean_squared_error', optimizer='rmsprop', metrics=['accuracy'])
In [29]:
hist = train(model, checkpoint_path=path.format('rmsprop_100epochs'), epochs=100)
Train on 1712 samples, validate on 428 samples
Epoch 1/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0197 - acc: 0.6209Epoch 00000: val_loss improved from inf to 0.00831, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 25s - loss: 0.0196 - acc: 0.6221 - val_loss: 0.0083 - val_acc: 0.6963
Epoch 2/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0083 - acc: 0.6728Epoch 00001: val_loss improved from 0.00831 to 0.00445, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 21s - loss: 0.0083 - acc: 0.6723 - val_loss: 0.0044 - val_acc: 0.6963
Epoch 3/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0071 - acc: 0.6975Epoch 00002: val_loss improved from 0.00445 to 0.00395, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 22s - loss: 0.0071 - acc: 0.6951 - val_loss: 0.0040 - val_acc: 0.6963
Epoch 4/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0057 - acc: 0.7028Epoch 00003: val_loss did not improve
1712/1712 [==============================] - 23s - loss: 0.0057 - acc: 0.7004 - val_loss: 0.0054 - val_acc: 0.6963
Epoch 5/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0052 - acc: 0.7052Epoch 00004: val_loss improved from 0.00395 to 0.00305, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 25s - loss: 0.0052 - acc: 0.7044 - val_loss: 0.0031 - val_acc: 0.6963
Epoch 6/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0043 - acc: 0.7034Epoch 00005: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 0.0043 - acc: 0.7033 - val_loss: 0.0032 - val_acc: 0.7033
Epoch 7/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0038 - acc: 0.7064Epoch 00006: val_loss improved from 0.00305 to 0.00248, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 20s - loss: 0.0038 - acc: 0.7079 - val_loss: 0.0025 - val_acc: 0.6986
Epoch 8/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0033 - acc: 0.7117Epoch 00007: val_loss did not improve
1712/1712 [==============================] - 20s - loss: 0.0033 - acc: 0.7126 - val_loss: 0.0031 - val_acc: 0.6963
Epoch 9/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0030 - acc: 0.7223Epoch 00008: val_loss improved from 0.00248 to 0.00216, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 20s - loss: 0.0030 - acc: 0.7225 - val_loss: 0.0022 - val_acc: 0.7103
Epoch 10/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0028 - acc: 0.7188Epoch 00009: val_loss improved from 0.00216 to 0.00183, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 21s - loss: 0.0028 - acc: 0.7190 - val_loss: 0.0018 - val_acc: 0.7079
Epoch 11/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0027 - acc: 0.7223Epoch 00010: val_loss improved from 0.00183 to 0.00182, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 20s - loss: 0.0027 - acc: 0.7225 - val_loss: 0.0018 - val_acc: 0.7126
Epoch 12/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0025 - acc: 0.7164Epoch 00011: val_loss did not improve
1712/1712 [==============================] - 21s - loss: 0.0025 - acc: 0.7167 - val_loss: 0.0020 - val_acc: 0.7079
Epoch 13/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0024 - acc: 0.7246Epoch 00012: val_loss improved from 0.00182 to 0.00165, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 20s - loss: 0.0024 - acc: 0.7237 - val_loss: 0.0017 - val_acc: 0.7173
Epoch 14/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0023 - acc: 0.7258Epoch 00013: val_loss improved from 0.00165 to 0.00162, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 20s - loss: 0.0023 - acc: 0.7249 - val_loss: 0.0016 - val_acc: 0.7407
Epoch 15/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0022 - acc: 0.7235Epoch 00014: val_loss improved from 0.00162 to 0.00153, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 20s - loss: 0.0022 - acc: 0.7225 - val_loss: 0.0015 - val_acc: 0.7360
Epoch 16/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0021 - acc: 0.7512Epoch 00015: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 0.0021 - acc: 0.7506 - val_loss: 0.0016 - val_acc: 0.7313
Epoch 17/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0021 - acc: 0.7276Epoch 00016: val_loss improved from 0.00153 to 0.00138, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 20s - loss: 0.0021 - acc: 0.7266 - val_loss: 0.0014 - val_acc: 0.7290
Epoch 18/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0020 - acc: 0.7500Epoch 00017: val_loss did not improve
1712/1712 [==============================] - 23s - loss: 0.0020 - acc: 0.7512 - val_loss: 0.0016 - val_acc: 0.7126
Epoch 19/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0019 - acc: 0.7553Epoch 00018: val_loss did not improve
1712/1712 [==============================] - 23s - loss: 0.0019 - acc: 0.7547 - val_loss: 0.0014 - val_acc: 0.7313
Epoch 20/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0018 - acc: 0.7476Epoch 00019: val_loss did not improve
1712/1712 [==============================] - 21s - loss: 0.0018 - acc: 0.7471 - val_loss: 0.0015 - val_acc: 0.7523
Epoch 21/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0018 - acc: 0.7500Epoch 00020: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 0.0018 - acc: 0.7500 - val_loss: 0.0014 - val_acc: 0.7500
Epoch 22/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0017 - acc: 0.7636Epoch 00021: val_loss improved from 0.00138 to 0.00129, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 19s - loss: 0.0017 - acc: 0.7652 - val_loss: 0.0013 - val_acc: 0.7523
Epoch 23/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0016 - acc: 0.7700Epoch 00022: val_loss improved from 0.00129 to 0.00128, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 18s - loss: 0.0016 - acc: 0.7693 - val_loss: 0.0013 - val_acc: 0.7734
Epoch 24/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0016 - acc: 0.7765Epoch 00023: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 0.0016 - acc: 0.7751 - val_loss: 0.0014 - val_acc: 0.7430
Epoch 25/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0016 - acc: 0.7683Epoch 00024: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 0.0016 - acc: 0.7693 - val_loss: 0.0014 - val_acc: 0.7640
Epoch 26/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0015 - acc: 0.7695Epoch 00025: val_loss did not improve
1712/1712 [==============================] - 18s - loss: 0.0015 - acc: 0.7681 - val_loss: 0.0014 - val_acc: 0.7547
Epoch 27/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0015 - acc: 0.7807Epoch 00026: val_loss did not improve
1712/1712 [==============================] - 20s - loss: 0.0015 - acc: 0.7792 - val_loss: 0.0015 - val_acc: 0.7850
Epoch 28/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0014 - acc: 0.7706Epoch 00027: val_loss improved from 0.00128 to 0.00121, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 24s - loss: 0.0015 - acc: 0.7704 - val_loss: 0.0012 - val_acc: 0.7640
Epoch 29/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0014 - acc: 0.7842Epoch 00028: val_loss improved from 0.00121 to 0.00120, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 22s - loss: 0.0014 - acc: 0.7839 - val_loss: 0.0012 - val_acc: 0.7570
Epoch 30/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0014 - acc: 0.7754Epoch 00029: val_loss did not improve
1712/1712 [==============================] - 22s - loss: 0.0014 - acc: 0.7739 - val_loss: 0.0013 - val_acc: 0.7664
Epoch 31/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0013 - acc: 0.7830Epoch 00030: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 0.0013 - acc: 0.7833 - val_loss: 0.0024 - val_acc: 0.7921
Epoch 32/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0014 - acc: 0.7836Epoch 00031: val_loss did not improve
1712/1712 [==============================] - 20s - loss: 0.0014 - acc: 0.7839 - val_loss: 0.0013 - val_acc: 0.7617
Epoch 33/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0013 - acc: 0.7795Epoch 00032: val_loss improved from 0.00120 to 0.00119, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 21s - loss: 0.0013 - acc: 0.7804 - val_loss: 0.0012 - val_acc: 0.7710
Epoch 34/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0013 - acc: 0.7901Epoch 00033: val_loss did not improve
1712/1712 [==============================] - 20s - loss: 0.0013 - acc: 0.7915 - val_loss: 0.0015 - val_acc: 0.7967
Epoch 35/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0012 - acc: 0.7919Epoch 00034: val_loss did not improve
1712/1712 [==============================] - 20s - loss: 0.0012 - acc: 0.7915 - val_loss: 0.0013 - val_acc: 0.7804
Epoch 36/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0012 - acc: 0.7871Epoch 00035: val_loss improved from 0.00119 to 0.00112, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 24s - loss: 0.0012 - acc: 0.7880 - val_loss: 0.0011 - val_acc: 0.7921
Epoch 37/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0012 - acc: 0.7930Epoch 00036: val_loss did not improve
1712/1712 [==============================] - 24s - loss: 0.0012 - acc: 0.7932 - val_loss: 0.0015 - val_acc: 0.7710
Epoch 38/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0012 - acc: 0.8048Epoch 00037: val_loss did not improve
1712/1712 [==============================] - 24s - loss: 0.0012 - acc: 0.8049 - val_loss: 0.0012 - val_acc: 0.7921
Epoch 39/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0011 - acc: 0.8031Epoch 00038: val_loss improved from 0.00112 to 0.00107, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 20s - loss: 0.0011 - acc: 0.8037 - val_loss: 0.0011 - val_acc: 0.7757
Epoch 40/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0011 - acc: 0.8025Epoch 00039: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0011 - acc: 0.8026 - val_loss: 0.0011 - val_acc: 0.7991
Epoch 41/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0011 - acc: 0.8072Epoch 00040: val_loss improved from 0.00107 to 0.00107, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 14s - loss: 0.0011 - acc: 0.8061 - val_loss: 0.0011 - val_acc: 0.7617
Epoch 42/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0011 - acc: 0.7960Epoch 00041: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0011 - acc: 0.7979 - val_loss: 0.0012 - val_acc: 0.7827
Epoch 43/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0011 - acc: 0.7954Epoch 00042: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0011 - acc: 0.7950 - val_loss: 0.0012 - val_acc: 0.7991
Epoch 44/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0011 - acc: 0.8096Epoch 00043: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 0.0011 - acc: 0.8102 - val_loss: 0.0011 - val_acc: 0.7921
Epoch 45/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0010 - acc: 0.7930Epoch 00044: val_loss did not improve
1712/1712 [==============================] - 16s - loss: 0.0010 - acc: 0.7932 - val_loss: 0.0015 - val_acc: 0.8084
Epoch 46/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0011 - acc: 0.8048Epoch 00045: val_loss improved from 0.00107 to 0.00104, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 18s - loss: 0.0011 - acc: 0.8049 - val_loss: 0.0010 - val_acc: 0.8037
Epoch 47/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0010 - acc: 0.8066Epoch 00046: val_loss did not improve
1712/1712 [==============================] - 18s - loss: 0.0010 - acc: 0.8061 - val_loss: 0.0011 - val_acc: 0.7827
Epoch 48/100
1696/1712 [============================>.] - ETA: 0s - loss: 9.9492e-04 - acc: 0.8096Epoch 00047: val_loss did not improve
1712/1712 [==============================] - 16s - loss: 9.9365e-04 - acc: 0.8102 - val_loss: 0.0011 - val_acc: 0.7991
Epoch 49/100
1696/1712 [============================>.] - ETA: 0s - loss: 0.0010 - acc: 0.8013Epoch 00048: val_loss did not improve
1712/1712 [==============================] - 17s - loss: 0.0010 - acc: 0.8008 - val_loss: 0.0011 - val_acc: 0.7827
Epoch 50/100
1696/1712 [============================>.] - ETA: 0s - loss: 9.7609e-04 - acc: 0.8154Epoch 00049: val_loss did not improve
1712/1712 [==============================] - 16s - loss: 9.7521e-04 - acc: 0.8154 - val_loss: 0.0011 - val_acc: 0.7944
Epoch 51/100
1696/1712 [============================>.] - ETA: 0s - loss: 9.5258e-04 - acc: 0.8184Epoch 00050: val_loss did not improve
1712/1712 [==============================] - 16s - loss: 9.5042e-04 - acc: 0.8178 - val_loss: 0.0011 - val_acc: 0.7921
Epoch 52/100
1696/1712 [============================>.] - ETA: 0s - loss: 9.7883e-04 - acc: 0.8042Epoch 00051: val_loss improved from 0.00104 to 0.00099, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 18s - loss: 9.8017e-04 - acc: 0.8049 - val_loss: 9.8706e-04 - val_acc: 0.7991
Epoch 53/100
1696/1712 [============================>.] - ETA: 0s - loss: 9.4290e-04 - acc: 0.8149Epoch 00052: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 9.4503e-04 - acc: 0.8143 - val_loss: 0.0010 - val_acc: 0.8061
Epoch 54/100
1696/1712 [============================>.] - ETA: 0s - loss: 9.5193e-04 - acc: 0.8149Epoch 00053: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 9.5017e-04 - acc: 0.8154 - val_loss: 0.0012 - val_acc: 0.8178
Epoch 55/100
1696/1712 [============================>.] - ETA: 0s - loss: 9.2407e-04 - acc: 0.8202Epoch 00054: val_loss did not improve
1712/1712 [==============================] - 17s - loss: 9.3067e-04 - acc: 0.8183 - val_loss: 0.0010 - val_acc: 0.8107
Epoch 56/100
1696/1712 [============================>.] - ETA: 0s - loss: 9.2335e-04 - acc: 0.8225Epoch 00055: val_loss did not improve
1712/1712 [==============================] - 14s - loss: 9.2166e-04 - acc: 0.8230 - val_loss: 0.0011 - val_acc: 0.8014
Epoch 57/100
1696/1712 [============================>.] - ETA: 0s - loss: 9.0795e-04 - acc: 0.8196Epoch 00056: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 9.0792e-04 - acc: 0.8195 - val_loss: 0.0010 - val_acc: 0.7944
Epoch 58/100
1696/1712 [============================>.] - ETA: 0s - loss: 9.0219e-04 - acc: 0.8149Epoch 00057: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 9.0101e-04 - acc: 0.8154 - val_loss: 0.0012 - val_acc: 0.7897
Epoch 59/100
1696/1712 [============================>.] - ETA: 0s - loss: 8.9301e-04 - acc: 0.8160Epoch 00058: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 8.9099e-04 - acc: 0.8172 - val_loss: 0.0012 - val_acc: 0.7710
Epoch 60/100
1696/1712 [============================>.] - ETA: 0s - loss: 8.7900e-04 - acc: 0.8178Epoch 00059: val_loss did not improve
1712/1712 [==============================] - 16s - loss: 8.8137e-04 - acc: 0.8178 - val_loss: 0.0012 - val_acc: 0.7710
Epoch 61/100
1696/1712 [============================>.] - ETA: 0s - loss: 8.8058e-04 - acc: 0.8231Epoch 00060: val_loss did not improve
1712/1712 [==============================] - 15s - loss: 8.8038e-04 - acc: 0.8218 - val_loss: 0.0011 - val_acc: 0.8201
Epoch 62/100
1696/1712 [============================>.] - ETA: 0s - loss: 8.6416e-04 - acc: 0.8237Epoch 00061: val_loss did not improve
1712/1712 [==============================] - 16s - loss: 8.6199e-04 - acc: 0.8236 - val_loss: 0.0011 - val_acc: 0.8154
Epoch 63/100
1696/1712 [============================>.] - ETA: 0s - loss: 8.6508e-04 - acc: 0.8231Epoch 00062: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 8.6522e-04 - acc: 0.8230 - val_loss: 0.0012 - val_acc: 0.8178
Epoch 64/100
1696/1712 [============================>.] - ETA: 0s - loss: 8.7426e-04 - acc: 0.8379Epoch 00063: val_loss did not improve
1712/1712 [==============================] - 21s - loss: 8.7280e-04 - acc: 0.8382 - val_loss: 0.0012 - val_acc: 0.8107
Epoch 65/100
1696/1712 [============================>.] - ETA: 0s - loss: 8.5219e-04 - acc: 0.8237Epoch 00064: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 8.5065e-04 - acc: 0.8242 - val_loss: 0.0010 - val_acc: 0.8061
Epoch 66/100
1696/1712 [============================>.] - ETA: 0s - loss: 8.2705e-04 - acc: 0.8261Epoch 00065: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 8.2734e-04 - acc: 0.8265 - val_loss: 0.0011 - val_acc: 0.8248
Epoch 67/100
1696/1712 [============================>.] - ETA: 0s - loss: 8.1886e-04 - acc: 0.8196Epoch 00066: val_loss did not improve
1712/1712 [==============================] - 20s - loss: 8.1748e-04 - acc: 0.8195 - val_loss: 0.0011 - val_acc: 0.8084
Epoch 68/100
1696/1712 [============================>.] - ETA: 0s - loss: 8.4373e-04 - acc: 0.8267Epoch 00067: val_loss did not improve
1712/1712 [==============================] - 20s - loss: 8.4266e-04 - acc: 0.8259 - val_loss: 0.0010 - val_acc: 0.8201
Epoch 69/100
1696/1712 [============================>.] - ETA: 0s - loss: 8.4441e-04 - acc: 0.8249Epoch 00068: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 8.4405e-04 - acc: 0.8254 - val_loss: 0.0010 - val_acc: 0.7944
Epoch 70/100
1696/1712 [============================>.] - ETA: 0s - loss: 8.0846e-04 - acc: 0.8243Epoch 00069: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 8.0854e-04 - acc: 0.8259 - val_loss: 0.0010 - val_acc: 0.8131
Epoch 71/100
1696/1712 [============================>.] - ETA: 0s - loss: 8.1667e-04 - acc: 0.8308Epoch 00070: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 8.2139e-04 - acc: 0.8294 - val_loss: 0.0011 - val_acc: 0.8364
Epoch 72/100
1696/1712 [============================>.] - ETA: 0s - loss: 8.1224e-04 - acc: 0.8455Epoch 00071: val_loss did not improve
1712/1712 [==============================] - 20s - loss: 8.1095e-04 - acc: 0.8458 - val_loss: 0.0011 - val_acc: 0.8224
Epoch 73/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.9342e-04 - acc: 0.8261Epoch 00072: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 7.9335e-04 - acc: 0.8265 - val_loss: 0.0011 - val_acc: 0.8107
Epoch 74/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.8245e-04 - acc: 0.8373Epoch 00073: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 7.8162e-04 - acc: 0.8359 - val_loss: 0.0012 - val_acc: 0.8131
Epoch 75/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.9121e-04 - acc: 0.8267Epoch 00074: val_loss did not improve
1712/1712 [==============================] - 18s - loss: 7.9067e-04 - acc: 0.8277 - val_loss: 0.0011 - val_acc: 0.8061
Epoch 76/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.8547e-04 - acc: 0.8219Epoch 00075: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 7.8559e-04 - acc: 0.8213 - val_loss: 0.0012 - val_acc: 0.8084
Epoch 77/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.8081e-04 - acc: 0.8296Epoch 00076: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 7.7966e-04 - acc: 0.8294 - val_loss: 0.0011 - val_acc: 0.7967
Epoch 78/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.9707e-04 - acc: 0.8296Epoch 00077: val_loss improved from 0.00099 to 0.00097, saving model to saved_model/best_model_with_rmsprop_100epochs.hdf5
1712/1712 [==============================] - 19s - loss: 7.9695e-04 - acc: 0.8306 - val_loss: 9.6645e-04 - val_acc: 0.8201
Epoch 79/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.8413e-04 - acc: 0.8284Epoch 00078: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 7.8454e-04 - acc: 0.8277 - val_loss: 0.0011 - val_acc: 0.8178
Epoch 80/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.6499e-04 - acc: 0.8355Epoch 00079: val_loss did not improve
1712/1712 [==============================] - 20s - loss: 7.6467e-04 - acc: 0.8359 - val_loss: 0.0012 - val_acc: 0.8201
Epoch 81/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.7503e-04 - acc: 0.8231Epoch 00080: val_loss did not improve
1712/1712 [==============================] - 20s - loss: 7.7750e-04 - acc: 0.8236 - val_loss: 0.0010 - val_acc: 0.7967
Epoch 82/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.6843e-04 - acc: 0.8485Epoch 00081: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 7.6907e-04 - acc: 0.8475 - val_loss: 0.0010 - val_acc: 0.8131
Epoch 83/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.4811e-04 - acc: 0.8355Epoch 00082: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 7.5008e-04 - acc: 0.8353 - val_loss: 0.0012 - val_acc: 0.8131
Epoch 84/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.4937e-04 - acc: 0.8473Epoch 00083: val_loss did not improve
1712/1712 [==============================] - 18s - loss: 7.5031e-04 - acc: 0.8470 - val_loss: 0.0011 - val_acc: 0.8037
Epoch 85/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.6240e-04 - acc: 0.8320Epoch 00084: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 7.6188e-04 - acc: 0.8324 - val_loss: 0.0011 - val_acc: 0.8224
Epoch 86/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.5631e-04 - acc: 0.8337Epoch 00085: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 7.5622e-04 - acc: 0.8335 - val_loss: 0.0010 - val_acc: 0.8178
Epoch 87/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.5353e-04 - acc: 0.8438Epoch 00086: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 7.5392e-04 - acc: 0.8440 - val_loss: 9.9478e-04 - val_acc: 0.7967
Epoch 88/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.5036e-04 - acc: 0.8202Epoch 00087: val_loss did not improve
1712/1712 [==============================] - 20s - loss: 7.4995e-04 - acc: 0.8207 - val_loss: 0.0011 - val_acc: 0.7944
Epoch 89/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.5358e-04 - acc: 0.8178Epoch 00088: val_loss did not improve
1712/1712 [==============================] - 18s - loss: 7.5378e-04 - acc: 0.8178 - val_loss: 0.0010 - val_acc: 0.7921
Epoch 90/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.4046e-04 - acc: 0.8325Epoch 00089: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 7.4238e-04 - acc: 0.8329 - val_loss: 0.0011 - val_acc: 0.8178
Epoch 91/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.5638e-04 - acc: 0.8373Epoch 00090: val_loss did not improve
1712/1712 [==============================] - 19s - loss: 7.5985e-04 - acc: 0.8376 - val_loss: 9.8885e-04 - val_acc: 0.8037
Epoch 92/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.6020e-04 - acc: 0.8414Epoch 00091: val_loss did not improve
1712/1712 [==============================] - 18s - loss: 7.5926e-04 - acc: 0.8417 - val_loss: 9.6763e-04 - val_acc: 0.8084
Epoch 93/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.3095e-04 - acc: 0.8208Epoch 00092: val_loss did not improve
1712/1712 [==============================] - 18s - loss: 7.3076e-04 - acc: 0.8218 - val_loss: 0.0010 - val_acc: 0.7897
Epoch 94/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.3269e-04 - acc: 0.8449Epoch 00093: val_loss did not improve
1712/1712 [==============================] - 18s - loss: 7.3303e-04 - acc: 0.8446 - val_loss: 9.9579e-04 - val_acc: 0.7921
Epoch 95/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.2128e-04 - acc: 0.8479Epoch 00094: val_loss did not improve
1712/1712 [==============================] - 18s - loss: 7.2132e-04 - acc: 0.8475 - val_loss: 0.0010 - val_acc: 0.8154
Epoch 96/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.4410e-04 - acc: 0.8337Epoch 00095: val_loss did not improve
1712/1712 [==============================] - 17s - loss: 7.4185e-04 - acc: 0.8341 - val_loss: 0.0010 - val_acc: 0.8037
Epoch 97/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.5161e-04 - acc: 0.8443Epoch 00096: val_loss did not improve
1712/1712 [==============================] - 18s - loss: 7.5390e-04 - acc: 0.8440 - val_loss: 0.0011 - val_acc: 0.8084
Epoch 98/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.2537e-04 - acc: 0.8349Epoch 00097: val_loss did not improve
1712/1712 [==============================] - 18s - loss: 7.2611e-04 - acc: 0.8353 - val_loss: 0.0010 - val_acc: 0.7897
Epoch 99/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.2356e-04 - acc: 0.8567Epoch 00098: val_loss did not improve
1712/1712 [==============================] - 18s - loss: 7.2262e-04 - acc: 0.8546 - val_loss: 0.0011 - val_acc: 0.8061
Epoch 100/100
1696/1712 [============================>.] - ETA: 0s - loss: 7.4034e-04 - acc: 0.8438Epoch 00099: val_loss did not improve
1712/1712 [==============================] - 18s - loss: 7.3966e-04 - acc: 0.8435 - val_loss: 0.0011 - val_acc: 0.7874
In [31]:
model.load_weights(path.format('adam_100epochs'))

Step 7: Visualize the Loss and Test Predictions

(IMPLEMENTATION) Answer a few questions and visualize the loss

Question 1: Outline the steps you took to get to your final neural network architecture and your reasoning at each step.

Answer:

I first constructed the CNN (three convolutional layers with 16/32/64 filters) as I usually do, with a little modification: considering y is in the range of [-1, 1], I used tanh as the activation function of the last layer. I got the best loss of 0.0015 and accuracy of 0.7313 for validation set. Since the training speed didn't take too long, I increased the numbers of filters in the convolutional layers as this article suggested. Unfortunately, I didn't see a huge improvement (within 20 epochs) from the results (val_loss: 0.0014, val_acc: 0.7453), so I turned back to my first choice and increase the number of epochs.

I didn't use Dropout in the convolutional-layer part at the beginning because I remembered some article said it was not worth do to do so. In the spirit of test, I later added dropout after each max pooling layer, then I found it impaired the performance (val_loss dropped to 0.0028, val_acc to 0.6963 after 20 epochs) and I observed this network didn't suffer from overfitting in short time. I added Dropout to the fully connected layers as usual.

Question 2: Defend your choice of optimizer. Which optimizers did you test, and how did you determine which worked best?

Answer: I've run every given optimizer and plot the changes of their testing losses, validation losses and validation accuracies as below. Considering both validation loss and validation accuracy, RMSprop outperformed other optimizers

Use the code cell below to plot the training and validation loss of your neural network. You may find this resource useful.

In [33]:
def plot_histories(hists, labels, ylabels):
    def plot(hists, labels, ylabel):
        fig = plt.figure(figsize=(8, 8))
        ax = fig.add_subplot(111)
        
        ax.set_xlabel('epochs')
        ax.set_ylabel(ylabel)
        ax.set_yticks
        for (hist, label) in zip(hists, labels):
            ax.plot(hist.history[ylabel], label=label)
        ax.legend(loc='upper left')
        
        return fig, ax
    
    for ylabel in ylabels:
        plot(hists, labels, ylabel)
    
    return
In [31]:
## TODO: Visualize the training and validation loss of your neural network
hists = [hist_rmsprop, hist_adagrad, hist_adadelta, hist_adam, hist_adamax, hist_nadam]
labels = ['rmsprop', 'adagrad', 'adadelta', 'adam', 'adamax', 'nadam']
ylabels = ['loss', 'val_loss', 'val_acc']

plot_histories(hists, labels, ylabels)
In [34]:
plot_histories([hist], ['rmsprop'], ['val_loss', 'val_acc'])

Question 3: Do you notice any evidence of overfitting or underfitting in the above plot? If so, what steps have you taken to improve your model? Note that slight overfitting or underfitting will not hurt your chances of a successful submission, as long as you have attempted some solutions towards improving your model (such as regularization, dropout, increased/decreased number of layers, etc).

Answer: I haven't notice obvious overfitting when epochs is 30. When I set it to 100, the last part of val_loss seemed have tendency to increase. I used dropout to prevent overfitting, and ModelCheckpoint to save the model with least validation loss during the training

Visualize a Subset of the Test Predictions

Execute the code cell below to visualize your model's predicted keypoints on a subset of the testing images.

In [47]:
model.load_weights(path.format('adam_100epochs'))
y_test = model.predict(X_test)
fig = plt.figure(figsize=(20,20))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)
for i in range(9):
    ax = fig.add_subplot(3, 3, i + 1, xticks=[], yticks=[])
    plot_data(X_test[i], y_test[i], ax)

Step 8: Complete the pipeline

With the work you did in Sections 1 and 2 of this notebook, along with your freshly trained facial keypoint detector, you can now complete the full pipeline. That is given a color image containing a person or persons you can now

  • Detect the faces in this image automatically using OpenCV
  • Predict the facial keypoints in each face detected in the image
  • Paint predicted keypoints on each face detected

In this Subsection you will do just this!

(IMPLEMENTATION) Facial Keypoints Detector

Use the OpenCV face detection functionality you built in previous Sections to expand the functionality of your keypoints detector to color images with arbitrary size. Your function should perform the following steps

  1. Accept a color image.
  2. Convert the image to grayscale.
  3. Detect and crop the face contained in the image.
  4. Locate the facial keypoints in the cropped image.
  5. Overlay the facial keypoints in the original (color, uncropped) image.

Note: step 4 can be the trickiest because remember your convolutional network is only trained to detect facial keypoints in $96 \times 96$ grayscale images where each pixel was normalized to lie in the interval $[0,1]$, and remember that each facial keypoint was normalized during training to the interval $[-1,1]$. This means - practically speaking - to paint detected keypoints onto a test face you need to perform this same pre-processing to your candidate face - that is after detecting it you should resize it to $96 \times 96$ and normalize its values before feeding it into your facial keypoint detector. To be shown correctly on the original image the output keypoints from your detector then need to be shifted and re-normalized from the interval $[-1,1]$ to the width and height of your detected face.

When complete you should be able to produce example images like the one below

In [35]:
# Load in color image for face detection
image = cv2.imread('images/obamas4.jpg')


# Convert the image to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

image_copy = np.copy(image)

# plot our image
fig = plt.figure(figsize = (9,9))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])
ax1.set_title('image copy')
ax1.imshow(image_copy)
Out[35]:
<matplotlib.image.AxesImage at 0x123cbeb00>
In [42]:
def preprocess_image(image):
        gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
        gray = cv2.resize(gray, (96, 96))
        gray = gray/255
        gray = np.expand_dims(gray, axis=-1)

        return np.array([gray])
In [36]:
### TODO: Use the face detection code we saw in Section 1 with your trained conv-net 
## TODO : Paint the predicted keypoints on the test image

image_copy = np.copy(image)

def plot_faces_keypoints(image, scaleFactor=1.1, minNeighbors=6, model=model):
    
    faces = face_detector(image)
    
    for (x, y, w, h) in faces:
        cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 3)
        
        face_image = image[y: y+h, x: x+w]
        face_image = preprocess_image(face_image)
        
        model.load_weights(path.format('adam_100epochs'))
        keypoints = model.predict(face_image)
        
        keypoints = np.squeeze(keypoints)
        
        xs = keypoints[0::2] * w/2 + w/2 + x
        ys = keypoints[1::2] * h/2 + h/2 + y
        
        for (x, y) in zip(xs, ys):
            cv2.circle(image, (x, y), 1, (0, 255, 0), 3)
            
    return image

image_copy = plot_faces_keypoints(image_copy)
show(image, title='Original Image')
show(image_copy, title='Image with Detections')
Number of faces detected:  2

(Optional) Further Directions - add a filter using facial keypoints to your laptop camera

Now you can add facial keypoint detection to your laptop camera - as illustrated in the gif below.

The next Python cell contains the basic laptop video camera function used in the previous optional video exercises. Combine it with the functionality you developed for keypoint detection and marking in the previous exercise and you should be good to go!

In [54]:
import cv2
import time 
from keras.models import load_model
def laptop_camera_go():
    # Create instance of video capturer
    cv2.namedWindow("face detection activated")
    vc = cv2.VideoCapture(0)

    # Try to get the first frame
    if vc.isOpened(): 
        rval, frame = vc.read()
    else:
        rval = False
    
    # keep video stream open
    while rval:
        frame = plot_faces_keypoints(frame)
        # plot image from camera with detections marked
        cv2.imshow("face detection activated", frame)
        
        # exit functionality - press any key to exit laptop video
        key = cv2.waitKey(20)
        if key > 0: # exit by pressing any key
            # destroy windows
            cv2.destroyAllWindows()
            
            # hack from stack overflow for making sure window closes on osx --> https://stackoverflow.com/questions/6116564/destroywindow-does-not-close-window-on-mac-using-python-and-opencv
            for i in range (1,5):
                cv2.waitKey(1)
            return
        
        # read next frame
        time.sleep(0.05)             # control framerate for computation - default 20 frames per sec
        rval, frame = vc.read()  
In [55]:
# Run your keypoint face painter
laptop_camera_go()
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  1
Number of faces detected:  1
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  1
Number of faces detected:  1
Number of faces detected:  0
Number of faces detected:  1
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  1
Number of faces detected:  1
Number of faces detected:  1
Number of faces detected:  0
Number of faces detected:  1
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  1
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  0
Number of faces detected:  0
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-55-7aa27961689f> in <module>()
      1 # Run your keypoint face painter
----> 2 laptop_camera_go()

<ipython-input-54-0d2bccd6efeb> in laptop_camera_go()
     15     # keep video stream open
     16     while rval:
---> 17         frame = plot_faces_keypoints(frame)
     18         # plot image from camera with detections marked
     19         cv2.imshow("face detection activated", frame)

<ipython-input-53-b93c2d6f7c6e> in plot_faces_keypoints(image, scaleFactor, minNeighbors, model)
     13         return np.array([gray])
     14 
---> 15     faces = face_detector(image)
     16 
     17     for (x, y, w, h) in faces:

<ipython-input-12-9cc3c4f8fe71> in face_detector(image, scaleFactor, minNeighbors)
      6     face_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_frontalface_default.xml')
      7 
----> 8     faces = face_cascade.detectMultiScale(gray, scaleFactor, minNeighbors)
      9 
     10     print('Number of faces detected: ', len(faces))

KeyboardInterrupt: 

(Optional) Further Directions - add a filter using facial keypoints

Using your freshly minted facial keypoint detector pipeline you can now do things like add fun filters to a person's face automatically. In this optional exercise you can play around with adding sunglasses automatically to each individual's face in an image as shown in a demonstration image below.

To produce this effect an image of a pair of sunglasses shown in the Python cell below.

In [37]:
# Load in sunglasses image - note the usage of the special option
# cv2.IMREAD_UNCHANGED, this option is used because the sunglasses 
# image has a 4th channel that allows us to control how transparent each pixel in the image is
sunglasses = cv2.imread("images/sunglasses_4.png", cv2.IMREAD_UNCHANGED)

# Plot the image
fig = plt.figure(figsize = (6,6))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])
ax1.imshow(sunglasses)
ax1.axis('off');

This image is placed over each individual's face using the detected eye points to determine the location of the sunglasses, and eyebrow points to determine the size that the sunglasses should be for each person (one could also use the nose point to determine this).

Notice that this image actually has 4 channels, not just 3.

In [38]:
# Print out the shape of the sunglasses image
print ('The sunglasses image has shape: ' + str(np.shape(sunglasses)))
The sunglasses image has shape: (1123, 3064, 4)

It has the usual red, blue, and green channels any color image has, with the 4th channel representing the transparency level of each pixel in the image. Here's how the transparency channel works: the lower the value, the more transparent the pixel will become. The lower bound (completely transparent) is zero here, so any pixels set to 0 will not be seen.

This is how we can place this image of sunglasses on someone's face and still see the area around of their face where the sunglasses lie - because these pixels in the sunglasses image have been made completely transparent.

Lets check out the alpha channel of our sunglasses image in the next Python cell. Note because many of the pixels near the boundary are transparent we'll need to explicitly print out non-zero values if we want to see them.

In [39]:
# Print out the sunglasses transparency (alpha) channel
alpha_channel = sunglasses[:,:,3]
print ('the alpha channel here looks like')
print (alpha_channel)

# Just to double check that there are indeed non-zero values
# Let's find and print out every value greater than zero
values = np.where(alpha_channel != 0)
print ('\n the non-zero values of the alpha channel look like')
print (values)
the alpha channel here looks like
[[0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 0]
 ..., 
 [0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 0]]

 the non-zero values of the alpha channel look like
(array([  17,   17,   17, ..., 1109, 1109, 1109]), array([ 687,  688,  689, ..., 2376, 2377, 2378]))

This means that when we place this sunglasses image on top of another image, we can use the transparency channel as a filter to tell us which pixels to overlay on a new image (only the non-transparent ones with values greater than zero).

One last thing: it's helpful to understand which keypoint belongs to the eyes, mouth, etc. So, in the image below, we also display the index of each facial keypoint directly on the image so that you can tell which keypoints are for the eyes, eyebrows, etc.

With this information, you're well on your way to completing this filtering task! See if you can place the sunglasses automatically on the individuals in the image loaded in / shown in the next Python cell.

In [40]:
# Load in color image for face detection
image = cv2.imread('images/obamas4.jpg')

# Convert the image to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)


# Plot the image
fig = plt.figure(figsize = (8,8))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])
ax1.set_title('Original Image')
ax1.imshow(image)
Out[40]:
<matplotlib.image.AxesImage at 0x14624d978>
In [71]:
## (Optional) TODO: Use the face detection code we saw in Section 1 with your trained conv-net to put
## sunglasses on the individuals in our test image
image_copy = np.copy(image)

def wear_sunglasses(image, sunglasses=sunglasses, model=model):
    faces = face_detector(image)
    
    for (x, y, w, h) in faces:
        face_image = image[y: y+h, x: x+w]
        face_image = preprocess_image(face_image)
        
        model.load_weights(path.format('adam_100epochs'))
        keypoints = np.squeeze(model.predict(face_image))
        
        '''
        # return the coordinate of the specified corner based on the alpha channel
        def corner(image, y, x):
            assert(x == 'left' or x == 'right')
            assert(y == 'top' or y == 'bottom')

            corner = (0, 0)
            is_corner_found = False
            for i in range(0, image.shape[1])[::1 if x=='left' else -1]:
                if is_corner_found == True:
                    print('corner has been found')
                    break
                for j in range(0, image.shape[0])[::1 if y=='top' else -1]:
                    if image[j][i][3] != 0:
                        corner = (j, i)
                        is_corner_found = True
                        break
            return corner
        
        top_left_corner = corner(sunglasses, 'top', 'left')
        top_right_corner = corner(sunglasses, 'top', 'right')
        print('top left corner: ', top_left_corner)
        print('top right corner: ', top_right_corner)
        '''
        
        # denormalize a detected keypoint
        def denormalize(point, h, w):
            point = (point[0] * h/2 + h/2 + y, point[1] * w/2 + w/2 + x)
            return point
        
        point7 = (keypoints[13], keypoints[12])
        point9 = (keypoints[17], keypoints[16])
        
        point7 = np.int32(denormalize(point7, h, w))
        point9 = np.int32(denormalize(point9, h, w))
        print('point 7: ', point7)
        print('point 9: ', point9)
        
        top_left_corner = (0, 0)
        top_right_corner = (0, sunglasses.shape[1])
        
        # calculate resize rate
        rate = (point7[1]-point9[1])/(top_right_corner[1]-top_left_corner[1]) * 1.1
        print('rate: ', rate)
        resized_sunglasses = cv2.resize(sunglasses, (0, 0), fx=rate, fy=rate)
        print('sunglasses shape: ', sunglasses.shape)
        print('resized sunglasses shape: ', resized_sunglasses.shape)
        
        start_y, start_x = (point9[0] - magic_number[0], point9[1] - magic_number[1])
        
        print('start y: ', start_y)
        print('start x: ', start_x)
        
        sunglasses_h, sunglasses_w = resized_sunglasses.shape[0], resized_sunglasses.shape[1]
        
        
        replaced_area = image[start_y: start_y+sunglasses_h, start_x: start_x+sunglasses_w]
        
        replaced_area = replaced_area if resized_sunglasses[..., 3] == 0 else resized_sunglasses
        
wear_sunglasses(image_copy)
Number of faces detected:  2
point 7:  [129 282]
point 9:  [129 251]
rate:  0.0111292428198
sunglasses shape:  (1123, 3064, 4)
resized sunglasses shape:  (12, 34, 4)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-71-b5cba1b6c193> in <module>()
     73         replaced_area = replaced_area if resized_sunglasses[..., 3] == 0 else resized_sunglasses
     74 
---> 75 wear_sunglasses(image_copy)

<ipython-input-71-b5cba1b6c193> in wear_sunglasses(image, sunglasses, model)
     61         print('resized sunglasses shape: ', resized_sunglasses.shape)
     62 
---> 63         start_y, start_x = (point9[0] - magic_number[0], point9[1] - magic_number[1])
     64 
     65         print('start y: ', start_y)

NameError: name 'magic_number' is not defined

(Optional) Further Directions - add a filter using facial keypoints to your laptop camera

Now you can add the sunglasses filter to your laptop camera - as illustrated in the gif below.

The next Python cell contains the basic laptop video camera function used in the previous optional video exercises. Combine it with the functionality you developed for adding sunglasses to someone's face in the previous optional exercise and you should be good to go!

In [ ]:
import cv2
import time 
from keras.models import load_model
import numpy as np

def laptop_camera_go():
    # Create instance of video capturer
    cv2.namedWindow("face detection activated")
    vc = cv2.VideoCapture(0)

    # try to get the first frame
    if vc.isOpened(): 
        rval, frame = vc.read()
    else:
        rval = False
    
    # Keep video stream open
    while rval:
        # Plot image from camera with detections marked
        cv2.imshow("face detection activated", frame)
        
        # Exit functionality - press any key to exit laptop video
        key = cv2.waitKey(20)
        if key > 0: # exit by pressing any key
            # Destroy windows 
            cv2.destroyAllWindows()
            
            for i in range (1,5):
                cv2.waitKey(1)
            return
        
        # Read next frame
        time.sleep(0.05)             # control framerate for computation - default 20 frames per sec
        rval, frame = vc.read()    
        
In [ ]:
# Load facial landmark detector model
model = load_model('my_model.h5')

# Run sunglasses painter
laptop_camera_go()